Read in txt file to array[r][c].

Perhaps my original wording on this was confusing so I will ask a different way.

if I have a text file called

data.txt

and it has the following in it.

12345
67890
12345
67890

how would i read this information into an array called

int data[4][5]

This is in C.

Thanks.
Last edited on
anyone?
In C specifically? That would probably be the "getc()" function out of the stdio.h header file: http://www.cplusplus.com/reference/cstdio/getc/

You could store them in your array using embedded for loops.
That's what I'm looking into now. Just not having any luck. I have made a smaller data set and am working on reading it in, storing it in the array and then writing it back out. The out put never matches the input.
Here is a representation of what I am trying to fix. If I could get this to work correctly then I could apply it to the actual file I need to change. I can get it to read from the source file and write directly to the output file but when I try to read into the array and write out from the array it just doesn't happen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#pragma warning(disable:4996)

main(void)
{


	FILE *gridinput;
	FILE *gridoutput;

	char filenamein[] = "shortgrid.txt";
	char filenameout[] = "results.txt";

	#define	GRID_ROWS 20
	#define GRID_COLUMNS 10

	int grid[GRID_ROWS][GRID_COLUMNS];
	
	if ((gridinput = fopen(filenamein, "r")) == NULL)
	{
		fprintf(stderr, "Filename, %s, could not be open for read\n", gridinput);
		exit(-1);
	}

	if ((gridoutput = fopen(filenameout, "w")) == NULL)
	{
		fprintf(stderr, "Filename, %s, could not be open for write\n", gridoutput);
		exit(-1);
	}

 	int data;
	int rowcount;
	int columncount;

	while ((data = fgetc(gridinput)) != EOF)
	{
		
		for (rowcount = 0; rowcount < GRID_ROWS; rowcount++)
		{
			for (columncount = 0; columncount < GRID_COLUMNS; columncount++)
			{
				grid[rowcount][columncount] = data;
			}
		}
	}

	for (rowcount = 0; rowcount < GRID_ROWS; rowcount++)
	{
		for (columncount = 0; columncount < GRID_COLUMNS; columncount++)
		{
			printf("%d",&grid[rowcount][columncount]);
			fwrite(&grid[rowcount][columncount], 1, 1, gridoutput);
		}
		printf("\n");
	}
}


Here is a small data set to go with this file

1000011010
0011010111
0101001011
1010001101
0100010110
0101101101
0011001100
1101100011
0101001101
0101010101
0001100011
1100111100
1011000111
1000011111
0011100010
0011101100
1100001110
0101000111
1110011111
0001101010
1
2
3
4
5
6
7
8
9
10
11
	while ((data = fgetc(gridinput)) != EOF)
	{
		
		for (rowcount = 0; rowcount < GRID_ROWS; rowcount++)
		{
			for (columncount = 0; columncount < GRID_COLUMNS; columncount++)
			{
				grid[rowcount][columncount] = data;
			}
		}
	}


For every character in the file, set each and every element in grid to that element. At the end of the while, every element of grid is the last value read from the file.

Also, you're getting characters from the file. The character '1' does not have the value 1.


printf("%d",&grid[rowcount][columncount]);
Why are you printing the address of each element?
That's a nice rehashing of the problem but I did not notice any proposed solution. How do you restructure the incoming information so that each character read in is in its corresponding place in the array? How would you store the char being read from the text file as an int in the array? Where would the conversion take place?

The printing out part was just the last of several different (unsuccessful) attempts to see where something was going wrong.

Thanks
That's a nice rehashing of the problem but I did not notice any proposed solution. How do you restructure the incoming information so that each character read in is in its corresponding place in the array? How would you store the char being read from the text file as an int in the array? Where would the conversion take place?
You are supposed to develop the solution.

Write out what you want to do, step by step. Write out code to accomplish it. If you find you're writing multiple lines of code for a step, break the step down further. If you run into a problem, post a question.


How would you store the char being read from the text file as an int in the array? Where would the conversion take place?
grid[r][c] = data - '0';
I changed my loop to this..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for (rowcount = 0; rowcount < GRID_ROWS; rowcount++)
	{
		for (columncount = 0; columncount < GRID_COLUMNS; columncount++)
		{
			data = fgetc(gridinput);
			grid[rowcount][columncount] = data - '0';
		}
	}

	for (rowcount = 0; rowcount < GRID_ROWS; rowcount++)
	{
		for (columncount = 0; columncount < GRID_COLUMNS; columncount++)
		{
			printf("%d",grid[rowcount][columncount]);
			fwrite(&grid[rowcount][columncount], sizeof(int), 1, gridoutput);
		}
		printf("\n");
	}


but my output to the screen is showing some extra info from somthing

1000011010
-38001101011
1-3801010010
11-381010001
101-38010001
0110-3801011
01101-380011
001100-38110
1100011-3801
01001101-380
101010101-38
0001100011
-38110011110
0-3810110001
11-381000011
111-38001110
0010-3800111
01100-381100
001110-38010
1000111-3811

The -38 should not be there. Also there is no output to the file. I can probably get the output to the file corrected but I do not know what the -38 is. My guess is that it is picking up the \n and writing it also to the array. I just need the numbers. If there a way to tell it to ignore other things or specify just the o and 1?
Getting closer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// #include <cctype>

// ...

    unsigned rowcount = 0, columncount = 0 ;
    while ( rowcount < GRID_ROWS && columncount < GRID_COLUMNS )
    {
        int ch ;
        if ( (ch = fgetc(gridinput)) != '\n' )  // or: if ( std::isdigit(ch = fgetc(gridinput)) )
        {
            grid[rowcount][columncount] = ch - '0' ;

            if ( ++columncount == GRID_COLUMNS && ++rowcount < GRID_ROWS)
               columncount = 0;
        }
    }

//... 


Thanks for the pointers. Here is working file for future reference should anyone ever need to know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#pragma warning(disable:4996)

main(void)
{
	FILE *gridinput;
	FILE *gridoutput;

	char filenamein[] = "shortgrid.txt";
	char filenameout[] = "results.txt";

	#define	GRID_ROWS 20
	#define GRID_COLUMNS 10

	int grid[GRID_ROWS][GRID_COLUMNS];
	
	if ((gridinput = fopen(filenamein, "r")) == NULL)
	{
		fprintf(stderr, "Filename, %s, could not be open for read\n", gridinput);
		exit(-1);
	}

	if ((gridoutput = fopen(filenameout, "w")) == NULL)
	{
		fprintf(stderr, "Filename, %s, could not be open for write\n", gridoutput);
		exit(-1);
	}

 	int data;
	unsigned int rowcount;
	unsigned int columncount;

	rowcount = 0;
	columncount = 0;
	
	while (rowcount < GRID_ROWS && columncount < GRID_COLUMNS)
	{
		if ((data = fgetc(gridinput)) != '\n')
		{
			grid[rowcount][columncount] = data - '0';

			if (++columncount == GRID_COLUMNS && ++rowcount < GRID_ROWS)
				columncount = 0;
		}
	}

	for (rowcount = 0; rowcount < GRID_ROWS; rowcount++)
	{
		for (columncount = 0; columncount < GRID_COLUMNS; columncount++)
		{
			printf("%d",grid[rowcount][columncount]);
			fprintf(gridoutput, "%d", grid[rowcount][columncount]);
		}
		printf("\n");
		fprintf(gridoutput, "\n");
	}
}
Last edited on
Topic archived. No new replies allowed.