Thread: file problem (I have never seen this before)

  1. #1
    Unregistered
    Guest

    file problem (I have never seen this before)

    I have my code to read a text file and it reads the txt fine, but after the text, it displays these funny characters. Here's my code:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    
    int main()
    {
    	ifstream file("text.txt");
    	
    	char array[50]; 
    	int i=0; 
    
    	while((!file.eof())&&(i < 49)) 
    	{ 
    
    		file.get(array[i]); 
    		++i; 
    	} 
    
    	array [i +1] = '\n'; 
    
    	cout << array; 
    
    	return 0;
    }
    Here is my Text file:
    Code:
    one
    two
    three
    
    1
    2
    3
    Here is what I get when I compile/run my program:
    [CODE[one
    two
    three

    1
    2
    3_¦
    ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦tÇB[/CODE]

    Anybody know what's going on?

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    it could be because your array - array[50] when gets to eof is not complete with data ie some elements in the end are unfused and are hence filled with garbage so in the line

    cout << array;

    it displays those elements as pipe symbols,ascii characters.
    -

  3. #3
    Unregistered
    Guest
    I tried to get the size of the array, then only read those parts of it but it hasn't worked and my prog does the same thing. Here is my code:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <windows.h>
    
    int main()
    {
    	ifstream file("text.txt");
    	
    	char array[50]; 
    	int i = 0;
    	int x;
    	int length = 0;
    
    	while(!file.eof()) 
    	{ 
    
    		file.get(array[i]); 
    		i++; 
    	} 
    
    	length = sizeof(array);
    
    	for(x = 0; x < length; x++)
    	{
    		cout << array[x];
    	}
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    add this line to debug
    Code:
    length = sizeof(array);
    //insert next line
    cout << "Length: " << length << endl;
    see if this shows you the problem
    otherwise, i'll be happy to help fix

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >while((!file.eof())&&(i < 49))
    Checking for eof in the loop condition will cause your program to read too far, thus the garbage. There have been a lot of questions about this recently for some reason, a mystery of the universe I suppose.
    Code:
    while ( i < 49 ) {
      if ( file.eof() ) break;
      // Do stuff
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    the problem is in the output, which in a way is being cause by the input. prelude's code is a good way to get the input, but your character array should be null terminated to prevent displaying the "empty" locations that cause junk to be output.

    replace the beggining code of your orignal while loop with prelude's
    also replace> array [i +1] = '\n';
    with> array[i] = '\0';

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Or initialize the array to all nuls with memset
    memset ( array, '\0', SIZE );

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    >Or initialize the array to all nuls with memset
    oh? i'm gonna have to look up memset! (is it C or C++ command?)

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    memset

    -Prelude
    My best code is written with the delete key.

  10. #10
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    array [i +1] = '\n';

    is dangerous anyway, because you could potentially write to array[50] which doesn't exist...

    also...
    Code:
    	} 
    
    	array [i +1] = '\n'; 
    
    should be
    
    }
        file.close();
        array[i + 1] = '\0';  // your cout is probably 
                              // overrunning your array to the next
                              // available null character.
    Last edited by Betazep; 03-13-2002 at 11:56 AM.
    Blue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM