hi, i want to read some data from a file and then put them into an array, the following is my code, and i found it works very strange in Xcode, the extra character(does not exist in the file) will be read. i'm just wondering how could it happen?
And this is the content of text2.txt
And this is the output
the character '\' and '}' will be read...


Code:
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 50
#define MAXCHAR 100
int main(void){
FILE *fp;
int ch;
char array[MAXLINE+1][MAXCHAR+1];
int i = 0;
int j = 0;
//open the file
fp = fopen("/text2.txt", "r");
//get the first character of the file
ch = getc(fp);
/*check whether the character is equal to A, if not, get the next character
and check again till the character is equal to A */
while (ch != 'A') {
ch = getc(fp);
}
array[i][j] = ch;
j++;
ch = getc(fp);
while (ch != EOF) {
array[i][j] = ch;
j++;
if(ch == '\n'){
i++;
j = 0;
}
ch = getc(fp);
}
for(int x = 0; x <= i; x++){
for(int y = 0; y <= j; y++)
printf("%c", array[x][y]);
}
//close the file
fclose(fp);
return EXIT_SUCCESS;
}
And this is the content of text2.txt
HTML:
A B C
D E F G
And this is the output
HTML:
Last login: Thu Apr 23 01:23:38 on ttys000
/Users/Ian/Documents/UNSW-IT/Programming\ C/assignments/assignment2/connect/build/Debug/connect ; exit;
Ians-MacBook:~ Ian$ /Users/Ian/Documents/UNSW-IT/Programming\ C/assignments/assignment2/connect/build/Debug/connect ; exit;
A B C \
D E F G }logout
[Process completed]
the character '\' and '}' will be read...