file reading

This is a discussion on file reading within the C++ Programming forums, part of the General Programming Boards category; i have a file data.txt and i want to read the data form the file and write them to screen ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    file reading

    i have a file data.txt
    and i want to read the data form the file and write them to screen

    file data is :
    1 2 3 ;

    Code:
    int i,j=0,dat[256];
    ifstream fin;
    char *line,*data;
    
    fin.open("data.dat");
    fin.getline(line,256);
    
    while(fin)
    {
          i  = 0;       
         while(line[i] != ' ; ')
         {
                 k = 0;            
                 while(line[i] != ' ')     //while the char is not  space ex: 10 5 4;
                  {
                        data[k] = line[i];     
                   }    
                    k = 0;
                    dat[k] = atoi(data); //is that true??
          }
    }
    
    }

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    all this stuff is redundant and useless for you...

    if your class looks like this:

    1 2 3 ;

    then do this and you're don:

    Code:
    #include<fstream>
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    
    int main()
    {
        ifstream fin("data.txt");
        char temp;
    
        if(fin.fail())
       {
             cout << "Can't open file for input!!! \n";
             exit(1);
        }
    
    
        else
        {
             fin.get(temp);
    
             while(temp != ';')
             {
                   cout << temp << endl;
                   fin.get(temp);
              }
         }
    
        return 0;
    }

    since (like you said) you only want to read a character from the file and display it on the screen this would be sufficient for you...



    matheo917

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    6
    This is a small one (am a newbie ) reading every character from a textfile and displaying it on the screen.
    Not sure if this is what you are looking for but it might help.

    Code:
    # include <iostream.h>
    # include <fstream.h>
    # include <stdlib.h>
    
    void main()
    
    
    {
    	fstream bestand;
    	char letter;
    	bestand.open ("c:\\data.txt",ios::in);
    	while(!bestand.eof())
    	{
    		bestand.get(letter);
    		cout<<letter;
    	}
    	bestand.close();
    }
    ooops, forgot the code tag sorry
    Last edited by The Wiep; 12-29-2002 at 10:17 AM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main()
    main only returns int, anything else is wrong and undefined.

    >bestand.open ("c:\\data.txt",ios::in);
    Be sure to check that this succeeded.

    >while(!bestand.eof())
    This is the wrong way to use eof(), it's better to use the input call as a loop condition since they tend to return correct boolean values:
    Code:
    while ( bestand.get ( letter ) )
    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
      char c;
      std::ifstream in ( "c:\\data.txt" );
      
      if ( in.is_open() ) {
        while ( in.get ( c ) )
          std::cout<< c <<std::flush;
        
        in.close();
      }
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    6
    Thanks for the correction.

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-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01: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, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21