Thread: file reading - ... - help!

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question file reading - ... - help!

    I am doing windows programming, but I still mess around with console programs. I ALSO USE Dev-C++ 4.01! Anyhow, I am messing around with fstream. I can use ofstream to write 9 random numbers to a file on new lines, but I can't read it. Here's the code:


    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <conio.c>
    #include <time.h>

    int main() {

    int rnd = 0 ;
    int nLine ;
    char numx ;

    cout << "Press any key..." ;
    getch() ;

    cout << "\n\n" ;

    for(int x=0 ; x < 9 ; x++)
    {
    rnd=rand() % 9 ;

    cout << "\nInputting " << rnd ;

    ofstream numbers ("numList.lst", ios::app) ;
    if( numbers.is_open() )
    {
    numbers << rnd << "\n" ;

    numbers.close() ;
    }
    }

    cout << "\n\n";

    ifstream fin ("numList.lst");

    while(! fin.eof() )
    {
    fin.getline(nLine, 10) ;

    cout << numx ;
    }

    getch() ;

    return 0 ;
    }


    When I try and use ifstream(fin) to read it, it outputs a bunch of numbers. How do I read it line-by-line and every time the file is a new line, put the new number on another line? ANY HELP IS APPRECIATED!
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    Unregistered
    Guest
    why do you read the data out as an int and then try to read it back as a char? It's okay to do it as long as you know that's what you are doing. In this case, since each value in the file is discrete and separated from each other by only a newline char, I think you can just read into numx directly.

    ifstream fin ("numList.lst");

    while(! fin.eof() )
    {
    fin >> numx ;

    cout << numx << endl; //note that I added an end of line symbol here! Maybe that's all you needed in your code, but I think the change in ifstream operator is cleaner.


    If file data were comma delimited or if there were a string you were trying to read in, like a name or address or phone number or something, then I would use getline. With simple data like this, the >> operator works just fine however.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    try this:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fstream.h>
    #include <conio.h>
    #include <time.h>
    
    int main(void)
    {
    	int rnd = 0;
    
    	// need a buffer to store the data, since you
    	// are using int, you don't need a buffer any bigger than
    	// 12 chars
    	char input[12];
    
    	ofstream numbers("numList.lst");
    	
    	if(numbers.is_open())
    	{
    		for(int x = 0; x < 9; x++)
    		{
    			rnd = rand() % 9;
    			
    			cout << "Inputting " << rnd << "\n";
    			
    			numbers << rnd << "\n" ;
    
    		}
    
    		numbers.close();
    	}
    
    	cout << "\n\n";
    
    	ifstream fin("numList.lst");
    
    	while(!fin.eof())
    	{
    		fin.getline(input, sizeof(input));
    
    		if(strlen(input) > 0)
    		{
    			// output as string, then as a number
    			cout << input << ", " << atoi(input) << "\n";
    		}
    	}
    
    	fin.close();
    
    	return(0);
    }
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM