File I/O difficulty

This is a discussion on File I/O difficulty within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to open a file called new with say any variable numbers of integers say 3 9 ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    21

    File I/O difficulty

    Hi,

    I am trying to open a file called new with say any variable numbers of integers say 3 9 4 1 4 or 2 5 1 4, I am able to read in the data, but since my size is fixed to 11 by the #define size 11 macro, I read 3 9 4 1 4 0 0 0 0 0 0 followed by extra zeros which I don't want to be read, when I just am trying to get
    the exact 3 9 4 1 4 to be read, or 2 5 1 4 instead of 2 5 1 4 0 0 0 0 0 0 0. If anyone can help that would be great, I've attached my code. Thanks for any help.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #define size 11
    
    using namespace std;
    
    
    int main(int argc, char *argv[])
    {
       
        ifstream infile;
        int i, j, total;
        int *num;
        num = new int[size];
        infile.open("new.txt");
        
       
        while (!infile.eof() ) {
        
      for (i=1; i <= size ; i++){
      
         infile >> num[i];
    
        }
               
               }
        infile.close();
       for (i = 1; i <= size; i++){
        cout << num[i] << " ";
    }
    delete [] num;
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    1) Are you aware that arrays start at index position 0? In this code, you are starting at index position 1:
    Code:
    for (i=1; i <= size ; i++){
      
         infile >> num[i];
    
        }
    How many digits do you think that code reads in? How many do you want to read in?

    2)
    I am able to read in the data, but since my size is fixed to 11 by the #define size 11 macro, I read 3 9 4 1 4 0 0 0 0 0 0 followed by extra zeros which I don't want to be read, when I just am trying to get
    the exact 3 9 4 1 4 to be read, or 2 5 1 4 instead of 2 5 1 4 0 0 0 0 0 0 0.
    What do you propose to have in the empty array spots rather than 0's? Junk integers?

    3) Do you only want to read from the file once?

    4) Is your ultimate goal to get the first 11 digits into an int variable, or do you need to keep them in an array?
    Last edited by 7stud; 04-09-2005 at 05:11 AM.

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Try something like this.

    HOWEVER, remember the number is stored as 'characters' NOT as an integer.
    Use the atoi function if you actually want to add or subtract your numbers etc.

    Code:
    #include<iostream>
    #include<fstream>
    #include<string.h>
    
    using namespace std;
    
    int main()
    {
        fstream file_pointer;
        file_pointer.open("new.txt",ios::in);
        
        char array[81];
        do{
            
            
            file_pointer>>array;
            cout<<array<<endl;
            
            
            
            
            cout<<"The first number is "<<array[0]<<endl;
            
            
            
            
            
          }while(file_pointer.peek()!=EOF); 
          
             
             
           int stop;
           cin>>stop;  
             
    }

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,199
    Actually when using the >> operator you can do as he did. for example this program will provide the wanted output:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	ifstream fin("test.txt");
    	int num;
    	while(fin >> num)
    		cout << num << endl;
    }
    In my test.txt I have 10 20 30 40 152 1 4835, which is displayed correctly.

    And you can add and substract them since they are integers in the program.

    Now to the original problem. I think what you want is a std::vector. Look it up at say www.cppreference.com
    Last edited by Shakti; 04-09-2005 at 06:25 AM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I don't really understand this post either:
    Quote Originally Posted by treenef
    Try something like this.

    HOWEVER, remember the number is stored as 'characters' NOT as an integer.
    Use the atoi function if you actually want to add or subtract your numbers etc.

    Code:
    #include<iostream>
    #include<fstream>
    #include<string.h>
    
    using namespace std;
    
    int main()
    {
        fstream file_pointer;
        file_pointer.open("new.txt",ios::in);
        
        char array[81];
        do{
            
            
            file_pointer>>array;
            cout<<array<<endl;
            
            
            
            
            cout<<"The first number is "<<array[0]<<endl;
            
            
            
            
            
          }while(file_pointer.peek()!=EOF); 
          
             
             
           int stop;
           cin>>stop;  
             
    }
    1)This:

    file_pointer>>array;

    continually reads one digit into array, and it overwrites the digit that was in array previously. So, after you have read in the whole file, there will be only one digit in array. Also, what's the point of reading in digits as chars, and then converting back to ints? Why not just read in the digits as ints, as the poster did?

    2) How did you decide to declare array with a size of 81?

    3)
    Code:
    while(file_pointer.peek()!=EOF);
    That is a very inefficient way to loop through a file. It requires reading each number twice: once to peek, and once to actually read it in. This is what I generally do, but I don't know if it's the best way(everybody seems to do it differently ):
    Code:
    if(!inFile.eof())
    {
    
    }
    But, when reading chars from a file using cin>> (or if there is a space after the last data in a file) there is a little problem actually reaching eof, so you have to do something like this:

    Code:
    inFile>>a_char;
    while(!inFile.eof())
    {
          inFile>>a_char;
    }
    4) This:
    Code:
    int stop;
           cin>>stop;
    isn't exactly a user friendly way to keep the console window from disappearing. If I hit return, nothing happens. I have to specifically type in an int to get the program to end.
    Last edited by 7stud; 04-10-2005 at 02:33 AM.

  6. #6
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    while(file_pointer.peek()!=EOF);


    That is a very inefficient way to loop through a file. It requires reading each number twice: once to peek, and once to actually read it in. This is what I generally do, but I don't know if it's the best way(everybody seems to do it differently ):
    I never realised I was reading it in twice. I just use what I got in my book.

    And yes you don't need the char stuff if you are only reading in numbers. Oops


    And the bit about using cin>>stop instead of system pause. Yes that would prob be better but I'm not fussed.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    And the bit about using cin>>stop instead of system pause. Yes that would prob be better but I'm not fussed.
    System pause is system dependent, so it isn't recommended. You can simply use:

    cin.get();

    and then any keypress will end the program. However, there are some tricky problems with that too, and now I realize that the method you use, while not user friendly, avoids those problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 03:47 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM

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