none
ifstream get(char) function

    Question

  • I have problems with the ifstream in my program. What I want this part to do, is reading characters from a file called "wordlist.txt" and put them into the array "char wordlist[100][2][20]". Each word is supposed to have an opposit word in Swedish. Therefore, the left index contains a pair (middle index) of words (right index) whose lenght is limited to 20 characters. If I display the array I will see the words from "wordlist.txt" but there are also loads of garbage characters in the array. What is wrong?

    From wordlist.cpp

    	char wordlist[100][2][20]; //this array will hold the wordlist
    	int x, y, z; //the index of the array
    
    	ifstream in("wordlist.txt"); //opening wordlist
    	if(!in) {
    		cout << "Error, cannot open file \"wordlist.txt\"!" << endl;
    		system("pause");
    		return 1;
    	}
    
    	cout << "Loading wordlist from \"wordlist.txt\"...\n\n";
    	for(x=0; x<100; x++) {
    		for(y=0; y<2; y++) {
    			for(z=0; z<20; z++) {
    				in.get(wordlist[x][y][z]);
    
    				if(wordlist[x][y][z] == ' ' ||
    				  wordlist[x][y][z] == '\n')
    				{
    					break;
    				}
    			}
    			if(!strcmp(wordlist[x][y], "!end!")) goto LoadDone; //LoadDone is outside the outermost for loop
    		}
    	}
    	LoadDone:
    		in.close();
    

     

    Sample file wordlist.txt

    cat katt
    dog hund
    horse häst
    cow ko
    bird fågel
    !end!
    Friday, March 11, 2011 7:19 PM

Answers

  • >there are also loads of garbage characters in the array.

    How are you checking the contents?

    You don't appear to be nul-terminating the strings
    when they're loaded to the array. So the debugger
    or a loop to show the strings will not know where
    each string ends.

    >char wordlist[100][2][20];

    You are not initializing this array, so it will contain
    whatever is in that location of memory at the time the
    program is run. You should initialize the array to zeros.
    That should solve the nul-termination issue, and make
    displaying the strings in the array less cluttered.

    - Wayne
    • Marked as answer by Johan.A Friday, March 11, 2011 7:53 PM
    Friday, March 11, 2011 7:32 PM
  • "but there are also loads of garbage characters in the array."

    That's because you don't add the nul character at the end of those strings. You only need a small change:

    if (wordlist[x][y][z] == ' ' || wordlist[x][y][z] == '\n') {
      wordlist[x][y][z] = '\0';
      break;
    }
    
    
    • Marked as answer by Johan.A Friday, March 11, 2011 7:53 PM
    Friday, March 11, 2011 7:34 PM

All replies

  • >there are also loads of garbage characters in the array.

    How are you checking the contents?

    You don't appear to be nul-terminating the strings
    when they're loaded to the array. So the debugger
    or a loop to show the strings will not know where
    each string ends.

    >char wordlist[100][2][20];

    You are not initializing this array, so it will contain
    whatever is in that location of memory at the time the
    program is run. You should initialize the array to zeros.
    That should solve the nul-termination issue, and make
    displaying the strings in the array less cluttered.

    - Wayne
    • Marked as answer by Johan.A Friday, March 11, 2011 7:53 PM
    Friday, March 11, 2011 7:32 PM
  • "but there are also loads of garbage characters in the array."

    That's because you don't add the nul character at the end of those strings. You only need a small change:

    if (wordlist[x][y][z] == ' ' || wordlist[x][y][z] == '\n') {
      wordlist[x][y][z] = '\0';
      break;
    }
    
    
    • Marked as answer by Johan.A Friday, March 11, 2011 7:53 PM
    Friday, March 11, 2011 7:34 PM
  • Thank you both, so fast replies! My program works properly now :D
    Friday, March 11, 2011 7:53 PM