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 !
Read text file into array
Collapse
X
-
Tags: None
-
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.Originally posted by Ganon11Definitely. What kind of text file will you be reading? What kind of array to you want to generate?Comment
-
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.Originally posted by Ganon11Do you know how many values there will be ahead of time? Is the number of rows different from the number of columns?Comment
-
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):
3) Create and open your ifstream variable to the input fileCode:int arr[MAX_SIZE][MAX_SIZE];
4)a. FOR LOOP from 0 to MAX_SIZE - 1 (index r)Comment
-
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
-
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
-
It should beOriginally posted by dschulenburgThanks, 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];
}}
??
Also, you aren't accounting for the newline character at the end of each row.Code:array[i][j] = c;
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
-
Thnaks ! So can you alos write an entire line into an array ?
Originally posted by nmadctIt should be
Also, you aren't accounting for the newline character at the end of each row.Code:array[i][j] = c;
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
-
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.Originally posted by dschulenburgThnaks ! So can you alos write an entire line into an array ?Comment
Comment