Read text file into array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dschulenburg
    New Member
    • Oct 2006
    • 40

    Read text file into array

    Hi ! I know it's possible to use a nested for loop or something to read an array and print it into a text file. Is it also possible to read in a text file and then put the values in an array ? Thanks !
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Definitely. What kind of text file will you be reading? What kind of array to you want to generate?

    Comment

    • dschulenburg
      New Member
      • Oct 2006
      • 40

      #3
      Originally posted by Ganon11
      Definitely. What kind of text file will you be reading? What kind of array to you want to generate?
      Cool ! I want to read in a file called file.txt which consists of N rows and N columns filled with values. I will need an array[N][N] that I can then use in code.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Do you know how many values there will be ahead of time? Is the number of rows different from the number of columns?

        Comment

        • dschulenburg
          New Member
          • Oct 2006
          • 40

          #5
          Originally posted by Ganon11
          Do you know how many values there will be ahead of time? Is the number of rows different from the number of columns?
          Yes, I know the number of rows and columns and they will be equal, so I know that the text file will have e.g. 20*20 values.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            OK, then here's what you want to do:

            1) Define a constant MAX_SIZE to the number of rows/columns (in your example, 20)
            2) Create a(n) (insert type here) array of size N x N with the statement (int used for example):
            Code:
            int arr[MAX_SIZE][MAX_SIZE];
            3) Create and open your ifstream variable to the input file
            4)a. FOR LOOP from 0 to MAX_SIZE - 1 (index r)

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              4)b. FOR LOOP from 0 to MAX_SIZE - 1 (index c)
              4)c. Input into arr[r][c];

              Once the FOR loops finish, your array will be filled with the values from your input file.

              Sorry for the 2 posts...the forum error has struck again. It wouldn't let me tack this little bit onto the end of the previous post.

              Comment

              • dschulenburg
                New Member
                • Oct 2006
                • 40

                #8
                Thanks, will point 3 and 4 be something like...

                while(((c=fgetc (fp) !== EOF)
                For (i=0; i <maxsize; i++){
                For (j=0; j < maxsize; j++){
                c=array[i][j];
                }}

                ??

                Comment

                • nmadct
                  Recognized Expert New Member
                  • Jan 2007
                  • 83

                  #9
                  Originally posted by dschulenburg
                  Thanks, will point 3 and 4 be something like...

                  while(((c=fgetc (fp) !== EOF)
                  For (i=0; i <maxsize; i++){
                  For (j=0; j < maxsize; j++){
                  c=array[i][j];
                  }}

                  ??
                  It should be

                  Code:
                  array[i][j] = c;
                  Also, you aren't accounting for the newline character at the end of each row.

                  You are using maxsize as the column/row count. This will only work if you're guaranteed to always have the same number of rows/columns. If that count can change, you'll need to count the number of columns in the first row before storing anything. This would be easier if you read the text line-by-line (using fgets) and then iterated over the characters in the line, rather than reading one character at a time.

                  If you do use fgets, keep in mind that it reads an entire line and leaves the newline character as the last character of the line. BUT, your last line might not have a newline, so don't blindly expect to have a newline unless your file is guaranteed to end with one.

                  Comment

                  • dschulenburg
                    New Member
                    • Oct 2006
                    • 40

                    #10
                    Thnaks ! So can you alos write an entire line into an array ?

                    Originally posted by nmadct
                    It should be

                    Code:
                    array[i][j] = c;
                    Also, you aren't accounting for the newline character at the end of each row.

                    You are using maxsize as the column/row count. This will only work if you're guaranteed to always have the same number of rows/columns. If that count can change, you'll need to count the number of columns in the first row before storing anything. This would be easier if you read the text line-by-line (using fgets) and then iterated over the characters in the line, rather than reading one character at a time.

                    If you do use fgets, keep in mind that it reads an entire line and leaves the newline character as the last character of the line. BUT, your last line might not have a newline, so don't blindly expect to have a newline unless your file is guaranteed to end with one.

                    Comment

                    • nmadct
                      Recognized Expert New Member
                      • Jan 2007
                      • 83

                      #11
                      Originally posted by dschulenburg
                      Thnaks ! So can you alos write an entire line into an array ?
                      You can make a loop to write a whole line to the array one character at a time, or you can use a function like strncpy or memcpy to copy the characters in bulk. No matter what you do, just be sure you don't go off the end of the array you're writing to.

                      Comment

                      Working...