can't read file into 2D array - c

I know my code has a lot of issues, but in the first place, I want to know this.
For example, my code is named example.c. It takes as an input a txt file, lets say txt.txt . I run the command ./example txt.txt in a terminal (linux).
According to what the user gives me through the file, I create a 2D array.
If the fcontext of the fie is:
+........- , I count the lines (in this example 1) and the elements before the new line, to find the rows of my array. Successfully I count them in my program. I find them lets say 1 lines 10 rows. However, I cant create the array. The values arent stored even thought I found correctly the lines and the rows of it. I suspect I dont read correctly the file.
Can you please tell me what i do wrong in the reading of the file into a 2d array?

Only this initially, I know i have a lot to do.
thanks again!

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  Put the code you need help with here.
#include <stdio.h>
#include <stdlib.h>
#define MAX 4000000
#define MAX1 1000
#define MAX2 1000
int timeg=0, end=0;
char array[MAX1][MAX2];
int N=0, M=0; //char a, b;
char queue_array[MAX], queue_array2[MAX], queue_array3[MAX];
int rear = - 1, rear2 = -1,rear3 = -1;
int front = - 1, front2 = - 1,front3 = - 1;

insert(char item, int i, int j, int t){
    if (rear == MAX - 1) printf("Queue Overflow \n");
    else{
        if (front == - 1){ //initially empty queue
        front = 0;
        front2=0;
        front3=0;
        }
        rear = rear + 1;
        queue_array[rear] = item;
        rear2 = rear2 + 1;
        queue_array2[rear2]=i;
        rear3 = rear3 + 1;
        queue_array3[rear3]=j;
        timeg=t;
    }
}
delete(char *qa,int *aa, int *bb, int *timegg){
    if (front == - 1 || front > rear) {
        printf("Queue Underflow \n");
        return ;
    }
    else
    {
        printf("Element deleted from queue is : %c\n", queue_array[front]);
        *qa=queue_array[front];//to stoixeio pou deleted
        *aa=queue_array2[front2];// x syntetagmenh del elem
        *bb=queue_array3[front3];// y coordinate of del elem
        *timegg=timeg;
        front = front + 1;
        front2=front2 + 1;
 front2=front2 + 1;
        front3=front3 + 1;
        //timeg=timeg+1;
    }
}
int floodfill(int x, int y, char del, int t){
     char el; int synteleia=0; char neo;
     if(x - 1 >= 0 && array[x - 1][y] == '.'){
        floodfill(x-1, y, del, t+1);
        el=array[x-1][y];
        insert(el, x-1, y, t+1);

     }
     if(y - 1 >= 0 && array[x][y - 1] == '.'){
        floodfill(x, y - 1, del, t+1);
        el=array[x][y-1];
        insert(el, x, y-1, t+1);
     }
     if(x + 1 <N  && array[x + 1][y] == '.'){
        floodfill(x + 1, y, del, t+1);
        el=array[x+1][y];
        insert(el, x+1, y, t+1);
     }
     if(y + 1 < M && array[x][y + 1] == '.'){
        floodfill(x, y + 1, del, t+1);
        el=array[x][y+1];
        insert(el, x, y+1, t+1);
     }
    
        if(x - 1 >= 0 && array[x - 1][y] !=del && array[x][y + 1] != 'X' && array[x][y + 1] != '.' ) {
        neo=array[x-1][y];
        neo='*';
        floodfill(x-1, y, neo, t+1);
        end=t+1;
        synteleia=1;
    }
    if(y - 1 >= 0 && array[x][y - 1] !=del && array[x][y + 1] != 'X' && array[x][y + 1] != '.') {
        neo=array[x][y-1];
        neo='*';
        floodfill(x, y - 1, neo, t+1);
        end=t+1;
        synteleia=1;
    }
    if(x + 1 < N && array[x + 1][y] !=del && array[x][y + 1] != 'X' && array[x][y + 1] != '.' ) {
         neo=array[x+1][y];
         neo='*';
         floodfill(x + 1, y, neo, t+1);
         end=t+1;
         synteleia=1;
    }
    if(y + 1 < M && array[x][y + 1] !=del && array[x][y + 1] != 'X' && array[x][y + 1] != '.' ) {
         neo=array[x][y+1];
         neo='*';
         floodfill(x, y + 1, neo, t+1);
         end=t+1;
         synteleia=1;
    }
     else
        return synteleia;
    }
    int main(int argc, char **argv){
    int rows=0, lines=0, j,k, x, y, val=0, a, b;
    char ch, mol, del, d;  int c, timef;
    /*char **array; int i;
    array=(char **) malloc(1000*sizeof(char *));
    for(i=0;i<1000;i++)
    array[i]=(char *) malloc(1000*sizeof(char));*/
    FILE *fin;
                        
 if(argc!=2){
        exit(2);
    }
    fin=fopen(argv[1],"r");
    if(fin==NULL) {
        exit(2);
    }
int words;
while ((ch = fgetc(fin)) != EOF)
    {
        rows++;

        /* Check new line */
        if (ch == '\n' || ch == '\0')
            lines++;

        /* Check words */
        if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
            words++;

    }

    /* Increment words and lines for last word */
    if (rows > 0)
    {
        words++;
        lines++;
    }

 /*   while(!feof(fin)){
        ch=fgetc(fin);
        if(ch=='\n') lines++;
    }
    while((ch=getc(fin))!=EOF){
 if(ch=='+' ||ch=='-'|| ch=='X')     rows++;
}
     while(!feof(fin)){
        ch=fgetc(fin);
        if(ch!=EOF && ch!='\n') rows++;
        if(ch=='\n') break;
    }*/
   while(!feof(fin)) {
   for(j=0; j<lines; j++){
        for(k=0; k<rows-1; k++){

            fscanf(fin, "%c", &array[j][k]);

}

}
}
for(j=0; j<lines; j++){
            for(k=0; k<rows-1; k++){
              printf("%c", array[j][k]);}
}
//rintf("c\n", array[j][k]);
printf("%d\n", lines);
    printf("%d\n", rows);

     for(j=0; j<lines; j++){
        for(k=0; k<rows-1; k++){
              if(array[j][k]=='+' || array[j][k]=='-') {
                   mol=array[j][k];
                   int t=0;
                   insert(mol, j, k, t);
              }
        }
}
//printf("%c\n", array[j][k]);
     while(front!=-1){
     delete(&d, &a, &b, &c);
     x=a; y=b; del=d; timef=c; //timef++;
     //printf("%d %d %c\n",a,b, del);
     val=floodfill(x, y, del, timef);
     if(val==1){
         printf("%d\n" , end);
         for(j=0; j<N; j++)
            for(k=0; k<M; k++)
              printf("%c", array[j][k]);

         break;
         }
       }
 //   free(array);
    fclose(fin);
    return 0;
}
                                              
Last edited on
while(!feof(fin)) {
for(j=0; j<lines; j++){
for(k=0; k<rows-1; k++){

fscanf(fin, "%c", &array[j][k]);

this is the problematic part
Here's how I got some hints to what you need to do next just by placing debugging statements in the code:

I stuck a printf statement in there. No output from that statement. I stuck more in until finally I saw something.. that part you mentioned doesn't seem to be running at all, so I wanted to find out where that problem began. I worked backwards thru the code until one of my printf statements actually produced output.

Doing something on a junk file I made, it produces output of numbers. 4 and 49... I see you placed some debug statements of your own to confirm it was getting that right sizing. that's good, because you confirmed that the file was read! You just needed to keep doing that exact same thing some more, and you would have found the problem. ^5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//debug
     printf("um hello\n");
//
   while(!feof(fin)) {
//debug
              printf("LOOK: lines = %i\n", lines);
              printf("LOOK: %i\n", rows);
//

   for(j=0; j<lines; j++){
        for(k=0; k<rows-1; k++){

            fscanf(fin, "%c", &array[j][k]);
//debug
              printf("LOOK: %c\n", array[j][k]);
// 



~/src/forums/maryt$ ./example.exe blah.txt
um hello
4
49


Looks like you read thru the entire file to find its structure above that code, then in the problem code, you want to read the file again based on that structure? But its already at the end of the file, so instead, nothing happens. Try closing it and re-opening it.
you are absolutely right!
I didnt think of that :/

Thank you very much :D
Topic archived. No new replies allowed.