Thread: Reading files

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    Reading files

    How can I read this line of numbers, assigning each to a different variable.

    1 3 4 7 2 3 5 1 2 4 1 1

    I need to account for the possability of some of the ints being double or tripple digit.

  2. #2
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Lightbulb Well...

    Im sorry, but u sound liek a newbie. Maybe you should get a book, or read some tutorials, or I could teach you if u want. Its not hard.

    SPH

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    well, ive been codeing about a year and a half, but I am self taught, so some things ive just never had to deal with. One of those is file I/O, so when it comes to that I am a newbie.

    I thought my question was rather simple and straitforward?

  4. #4
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Lightbulb wel...

    file io is much as any other io, if you do not understand hwo to do thisfrom a string, you will most likely not understand how it is being done from a file, for not understanding your own code is the worst thing of all. so I suggest you practise doing this with strings (by makking a class with a streampos to read along) and then I will give you some file io code using fstreams.

    SPH

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    I found this code some time back, I understand it, but it only demonstrates reading one value at a time.

    #include <fstream.h>
    #include <iostream.h>
    #include <stdlib.h>
    int main()
    {
    int array[10] = {1,7,3,6,2,6,6,2,8,1};

    //creating output stream object to be used when saving
    ofstream output;

    //opening file to write to
    output.open("file.txt");

    int i;

    //looping through all elements of the array and writing everything to the file
    for (i = 0; i < 10; i++)
    {
    output << array[i] << endl;
    }

    //closing the file
    output.close();

    //declare array to hold data
    int array2[10];

    //reset i to zero;
    i = 0;

    //declare ifstream, associate with file, and open file
    ifstream fin("file.txt");

    //check if opening file not successful
    if(!fin)
    {
    cout << "couldn't open file for reading." << endl;
    }

    //read in one int at a time into array2
    while(fin)
    {
    fin >> array2[i++];
    }

    //display array2 to show that read in was successful
    for(i = 0; i < 10; i++)
    {
    cout << array2[i] << endl;
    }

    return 0;
    }

  6. #6
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    wow... that looks lust like the example that i always use...

    well, here's how i do it.. it's probably the same as the above, but jsut incase something was missed.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>
    
    int main()
    {
     //creating input stream to read from file into program
     ifstream input;
     
     //creating output stream to write to a file from the program
    
     //opening file to read into the program using the previously created input stream.
     input.open("test.txt");
     
     //reading in the variables from the file and saving them into an array within the program
     cout << "how many different numbers are in the file?" << endl;
     int num_lines;
     cin >> num_lines;
     
     int array[num_lines];
    
     for (int i = 0; i < num_lines; i++)
     {
       cin >> array[i];
     }
    
     //whether to print the array to another file or to the screen
     char choice;
     do 
     { 
      cout << "print to [f]ile or print to [s]creen?" << endl;
      cin >> choice;
     }while (choice != 'f' && choice != 's'); 
     
     //printing the array to a file
     if (choice == 'f')
     {
       //creates an output stream to write to a file with
       ofstream output;
      
       //opens a file to write to  using the output stream
       output.open("ofile.txt"); 
    
       //cycles through the array and prints it to file
       for (i = 0; i < num_lines; i++)
       {
         output << array[i];
       }
      }
     
     //printing the array to the screen
     if (choice == 's')
     {
      //cycles through the array and print it to the screen
      for (i = 0; i < num_lines; i++)
      {
       cout << array[i];
      }
     }
    }
    you can use such functions as atof() and read in the numbers as strings and convert them to the appropriate format...

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    You cant do

    int array[num_lines];

    can you?

    I though you had to declare all array sizes at compile time, so the program will know how much memory to allocate, here you are declareing an array, and useing a variable to set the size. I tried something along that line a while back, and it never would work.

  8. #8
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I though you had to declare all array sizes at compile time
    You do, but some compilers (Dev C++ is one) will let you do what Aran has, but as it's highly non-standard, it's not very good practice.

    You could either manage your arrays dinamically using new and delete ,use something like the c++ vector template or use your own data structure.

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Thanks for backing me up zen, but that still dosent answer my question.

    how can i read this line of numbers from a file

    1 3 2 4 5 6 3

    and then this line useing the same code

    23 1 4 5 32 2 5

    If i just read in one charcter at a time, double digits will throw me off.

    Whats the answer?

  10. #10
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Talking okay

    Code:
    #incude <fstream.h>
    
    void main()
    {
        int n1,n2,n3,n4,n5;
        ifstream file("blah.txt");
        file>>n1>>n2>>n3>>n4>>n5;
        file.close();
    }
    I just hink you need to think for yourself a little.

    SPH

  11. #11
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    #include<iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        int num[10];
    	int i = 0;
        ifstream file("text.txt");
        while(file>>num[i++]);
    	file.close();
    	--i;
    	while(i)
    	{
    		cout << num[--i] << endl;
    
    	}
    	return 0;
    }
    I'm not a C++er so this could be improved but I would think that you would want to use an array for this or else a vector.

    Maybe I'll try to come up with the vector code.
    I compile code with:
    Visual Studio.NET beta2

  12. #12
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    #include<iostream>
    #include<fstream>
    #include<vector>
    using namespace std;
    
    int main()
    {
    	vector<int> v_numbers;
    	int x;
    	ifstream file("text.txt");
    	while(file >> x) v_numbers.push_back(x);
    	file.close();
    
    	for(vector<int>::size_type size = 0; size < v_numbers.size(); ++size)
    	{
    		cout << v_numbers[size] << endl;
    	}
    	return 0;
    }
    This is more the C++ers way!
    I compile code with:
    Visual Studio.NET beta2

  13. #13
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    I'm a C++er and i still don't know crap about vectors.... oooooo
    i just remembered the encryption system that i was going to program today.... *opens up dev-cpp and starts coding*

  14. #14
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    Read up on the different istream member functions/operators like >> and get() and getline(). They are used in both cout and any user defined streams like those used to read in files, etc. In a nutshell I use the >> operator if I want to read in a discrete variable delimited from other variables by whitespace and getline() when I want to read in an entire string that may (or may not) have embedded whitespace, parsing the input string when necessary.

  15. #15
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Thanks alot guys, I think I got it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  4. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM