about reading data from a file (C code) in Xcode

Discussion in 'Mac Programming' started by MacIan, Apr 22, 2009.

  1. MacIan macrumors newbie

    Joined:
    Apr 22, 2009
    Location:
    Sydney
    #1
    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?
    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...:confused::confused::confused:
     
  2. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    Edit: Never mind. I shouldn't post early in the morning after a night of almost no sleep...
     
  3. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    There are a few logic errors in your code.

    What happens if the file doesn't contain the letter 'A' at all?
    What happens if the file contains a very long line?
    What happens if the very last character in the file is a '\n'?
    What happens if the very last character in the file is not a '\n'? (You'll have to get a proper C textbook for that one).

    Try with a larger file that has lines of different lengths. Notice anything strange?
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Don't just give it away! =) This is clearly homework. If i'm reading the schedule right, the University of New South Wales' Semester 1 started about 6 weeks ago. I assume this is an intro class, but 6 weeks in hopefully off-by-one errors have been discussed and this is the sort of thing students should be working their way through this on their own (getting some help is fine, but it should be a nudge, not a giveaway). Not trying to criticize your willingness to help... posters should always state when things are homework.

    Unfortunately the file paths don't include a course number, only that this is assignment 2. I would say that based on the content, though, it must be an intro programming course.

    -Lee
     
  5. MacIan thread starter macrumors newbie

    Joined:
    Apr 22, 2009
    Location:
    Sydney
    #5
    Thanks for your advice, but it seems that the character '\' will be read before '\n' and there is no character '\' before a line break in the .txt file
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    Read through kpua and gnasher729's posts again, and read carefully through your code. The issues aren't that things that you can't see, don't exist, etc. are being read from the file, but instead that there are bugs in your code.

    -Lee

    EDIT: I don't know if your course has mentioned this explicitly, but at the end of a "C string", you need the null terminator, "\0". Unless you keep a list of lengths for each line so you know how many characters to print from each (based on the characters you found as you read through each line), you'll need to null terminate strings and print them with %s a line at a time, instead of %c a character at a time.
     
  7. MacIan thread starter macrumors newbie

    Joined:
    Apr 22, 2009
    Location:
    Sydney
    #7
    Thanks for that, but i do not quite understand because i'm a newer to programming :)

    i set some break points, and this is the content of my array after 10 loops
    [​IMG]

    why array[0][7] = '\\' ?
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    A better question is:
    why do you care what's in array[0][7]? The first line of the file only has 5 characters.

    -Lee
     
  9. MacIan thread starter macrumors newbie

    Joined:
    Apr 22, 2009
    Location:
    Sydney
    #9
    No, i think the first line of the file has 10 characters, including 5 spaces (there are 3 spaces after 'C')and 1 line break, does it?
     
  10. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    Just assume that I give you an input file, where the first line has 20 characters, the second line has 5 characters, and the third line has 12 characters, each followed by '\n' (which is just a way to write the "end of line" character in your program). And assume the very first letter is an 'A'.

    After your program has read the input file, what will be the value of j?
    How many characters will your program print from the first line, how many characters from the second line, and how many from the third line?

    Now assume the third line is _not_ followed by '\n'. What will the value of j be then? And how many characters from each line will your program print?

    A C program will not do what you want it to do. It will do _exactly_ what you tell it to do. In your case, what you want it to do and what you tell it to do are not the same thing. Don't try to find reasons why it _should_ work. The fact is, it doesn't work, so any reason that you might come up with why it should work _must_ necessarily be wrong.

    Just to confuse you: The input file quite likely doesn't contain the characters you think it contains. If your code was correct, that wouldn't matter. But your assumptions how many characters there are in each line are wrong. As I said, you are concentrating on the wrong thing. You try to figure out why the code should be working, when in fact it doesn't. Code isn't like your mother who tries to please you. Code will spot the slightest mistake you make and throw it in your face. And you made a few...
     
  11. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
    In the example you posted, the first line looks like:
    A_B_C__\n
    D_E_F_G___\n
    where _ is substituted for a space, and \n indicates a newline. I can't tell you what the actual file looks like, as I don't have access to it. If what you pasted is accurate, though, the first line will use positions 0-6 in array, so array[7] will have whatever you've put in there. If you've put nothing in there, it's just uninitialized memory, so it could have anything.

    Also based on what you have pasted, the second line is longer than the first, which means you have to store line length separately to properly print things.

    I don't know if your instructor prescribed storing the whole file in an array, then printing it, but if not another possible solution would just be to print a line at a time, then you don't have to worry about storing the lengths.

    Using the debugger is good, but it might be harder for you to visualize the whole run of your program. There is no shame at this level in putting in a print statement every other line that will tell you what the various variable values are, etc. so you can see exactly what's happening when your program runs.

    -Lee
     
  12. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #12
    Hi Lee,

    there will be extra fun if the file is a windows file ending in CR / LF pairs. :rolleyes:
     
  13. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #13
    There was some thread way-back-when where i could not reproduce the problem the user was having, because i was copying and pasting into vi, saving, and all was well. They swore up and down that it worked differently on their machine, so they eventually uploaded the file and it was windows-style line endings.

    Bonus fun if it's classic Mac OS style line endings.

    -Lee

    EDIT: Here it is: http://forums.macrumors.com/showthread.php?t=595649
    Turns out in that case it WAS classic Mac OS style line endings, not windows style.
     

Share This Page