reading from a file

This is a discussion on reading from a file within the C++ Programming forums, part of the General Programming Boards category; I have a beginner's question about reading data from file, but I've been unable to find any tutorial of FAQ ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    reading from a file

    I have a beginner's question about reading data from file, but I've been unable to find any tutorial of FAQ that would be useful for my problem, so please help

    Code:
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    int main()
    {
         int s;
         FILE *fp;
    
         fp = fopen("data.txt", "r"); 
         if(fp == NULL)
             cout<<"Error opening flie"<<endl;
         else{
             cout<<"Data in file:"<<endl;
             do{ 
                 s = fgetc(fp); 
                 cout<<s<<endl;
                }
             while( s != EOF );
             }
             
          fclose(fp);   
      
     system("PAUSE");
      return 0;   
    }
    what I get as a result is a series of numbers, but not the ones that are in data.txt
    the file that I want to read looks like this:
    Code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    but what I get as a result is this:
    Code:
    10
    57
    10
    47
    49
    10
    49
    49
    ...
    49
    54
    -1
    and there's 24 of them, not 16 like in the file I want to read.
    so, what am I doing wrong?
    thanks in advance

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    you're reading single characters.

    don't look for a tutorial or FAQ, just RTFM

    http://cplusplus.com/reference/clibrary/cstdio/fgetc/

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Or, since this appears to be C++, use the iostream approach to reading the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    Quote Originally Posted by m37h0d View Post
    you're reading single characters.

    don't look for a tutorial or FAQ, just RTFM

    fgetc - C++ Reference
    does that mean I can't read a number with fgetc? from what I can see the example in this link is pretty much the same as my program:
    Code:
    #include <stdio.h>
    int main ()
    {
      FILE * pFile;
      int c;
      int n = 0;
      pFile=fopen ("myfile.txt","r");
      if (pFile==NULL) perror ("Error opening file");
      else
      {
        do {
          c = fgetc (pFile);
          if (c == '$') n++;
        } while (c != EOF);
        fclose (pFile);
        printf ("The file contains %d dollar sign characters ($).\n",n);
      }
      return 0;
    }
    is it?
    it also reads int with fgetc.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    Quote Originally Posted by Salem View Post
    Or, since this appears to be C++, use the iostream approach to reading the file.
    OK, so how do I do that?

    is that with ifstream for reading from file and ofstream for writing into a file?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Code:
    ifstream in("myfile.txt");
    int var;
    while ( in >> var ) {
      cout << "Read in " << var << endl;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    I'm not sure if this is the best way to do it, but here is my attempt.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	fstream file;
    	file.open("test.txt");
    	string read;
    	while(getline(file, read))
    		cout << read << endl;	
    	
    	file.close();
    	
    	return 0;
    }
    Here is a link to fstream fstream - C++ Reference

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by crazyGauss View Post
    does that mean I can't read a number with fgetc? from what I can see the example in this link is pretty much the same as my program:
    Code:
    #include <stdio.h>
    int main ()
    {
      FILE * pFile;
      int c;
      int n = 0;
      pFile=fopen ("myfile.txt","r");
      if (pFile==NULL) perror ("Error opening file");
      else
      {
        do {
          c = fgetc (pFile);
          if (c == '$') n++;
        } while (c != EOF);
        fclose (pFile);
        printf ("The file contains %d dollar sign characters ($).\n",n);
      }
      return 0;
    }
    is it?
    it also reads int with fgetc.
    no it doesn't. it reads a character and assigns it to an int. there is a big difference, and unfortunately that type-abuse makes that a bad example imo.

    what you're reading, and then cout'ing is the raw byte values for the numerical codes of the text contained in your file.

    there is a big difference between int i =1; and char c = '1'; one is an actual integer value, and the other is a typographic representation of a particular character encoding scheme.

    to illustrate the difference, if, in your example, you were counting the instances of the number 1 in a text file, you would not have the logical condition:

    Code:
    if (c == 1) n++;
    but rather:

    Code:
    if (c == '1') n++;
    '1' has a numerical value, in ASCII, of 49. So if your input file were encoded in ASCII (which it probably should be), you could equivalently do:

    Code:
    if (c == 49) n++;

    because '1' == 49

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    Quote Originally Posted by Salem View Post
    Code:
    ifstream in("myfile.txt");
    int var;
    while ( in >> var ) {
      cout << "Read in " << var << endl;
    }
    wow, this one's realy simple, thanks
    OK, so now that I know this I want to read form a file like this:
    Code:
    1 10
    2 20
    3 30
    4 40
    5 50
    6 60
    7 70
    8 80
    9 90
    10 100
    11 110
    12 120
    13 130
    14 140
    15 150
    16 160
    and, I want only the second numbers (10, 20, 30, ...).
    so I've made this:
    Code:
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    int main()
    {     int i,j,var[40], d[40];
         
          i=0;
          j=0;
         
         ifstream in("example.txt");
         
         while ( in >> var[i] ) 
            {
             cout << "Read in " << var[i] << endl;         
             if(i%2)
              {
               d[j]=var[i];
               j++;
               }
             i++;      
             } 
          cout<<endl;         
      
         for(i=0;i<j;i++)
           {
            cout<<d[i]<<endl;             
            }                             
        cout<<endl;     
             
      system("PAUSE");
      return 0;
    }
    it works fine, but I'm not sure is there maybe a simpler way to do this, maybe while reading?
    and, also, how to make this into a function?
    I'll have to read the data from two different files, and store them is two different series.
    what I don't know is wether the series then need to be globaly defined or not, and what arguments should function have.
    what I've written here is an outline of what it should look like but I'm unsure about the part with the question marks.
    Code:
    #include<iostream>
    #include<fstream>
    #...
    
    using namespace std;
    
    
    function read(????)
    {
        this stuff for reading the file   
       return ???  
             }
    
    int main()
    {
        read(dat1.txt????, a[i]????);
        read(dat2.txt????, b[i]????);
        ...
    }

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    hey, I know it's not directly connected with the name of the thread, but it's not completely offtopic.

    when I read the file, and I want to check if the program has read it correctly, using cout I put them on the screen, but since I'm working with series of 3000+ numbers, the program doesn't display all off them, just the last 100 or so, and I can't scroll anymore up.

    is there any way to mend this?

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, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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