reading from files????????

This is a discussion on reading from files???????? within the C++ Programming forums, part of the General Programming Boards category; I need help i am making a program witch will write data to a file to store information and then ...

  1. #1
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148

    Question reading from files????????

    I need help i am making a program witch will write data to a file to store information and then later when used again can read the information and automaticly guess what user it is and put in his information.

    Now my problem is that i can make files but i cannot read from them. Can anyone show me an example program or what i have to type to make my program read all of the data in the file??

    thank you very much

    ps
    its for my dad's birthday

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    its for my dad's birthday
    How sentimental.

    Post your code.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    does this help?

    does this help?

    Code:
    #include <fstream>
    
    int main()
      {
         ifstream file; // file of type input file stream;
         file.open("filename.txt"); //open filename.txt
         string info;       
    
         file.getline(info, 50); // get first line of text file and store in info;
         
         cout << info;  //output info to screen.
         file.close();   // close the file.
        return 0;
      }
    i just wrote this off the top of my head.. its late here.. i could be completely off topic.. hope this helps in some small way anywayz..

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    [sarcasm]
    What a great gift idea! A console program that reads in a file and displays it! So many uses!
    [/sarcasm]

  5. #5
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    "its for my dads birthday! and my programming teacher needs a copy of it also, because it just so happens that my dad's birthday is on the exact same day as this assignment is due! isnt that cool?"
    I came up with a cool phrase to put down here, but i forgot it...

  6. #6
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    right all of u except tegwin, what is your problem!!!! first of all i dont have a freaking teacher, im only 13 and dont get that stuff yet

  7. #7
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    thank you tegwin, it helped alot i got it to read the first line of the file but what if i want it to read the second or the third??

    thank you again

    here is the code without your help. I tried inserting it but it gives me compile errors "string undifiened"

    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    int main()
    {
    char quit;  
    
        quit = '\0';
        while (quit != 'q')
        {
    int counter=0;
    int time;
    float amount;
    float intrest;
    cout<<"                          |---------------------|"<<endl;
    cout<<"                          |     Bank Pogram     |"<<endl;
    cout<<"                          |---------------------|"<<endl<<endl<<endl;
    ofstream a_file("data.txt", ios::app);
    cout<<"How much Money? ";
    cin>>amount;
    a_file<<"---------------------------------"<<endl<<endl<<"Start of Amount: $"<<amount;
    cout<<endl<<"What is the intrest rate? ";
    cin>>intrest;
    cout<<endl<<"How long are you keeping it there? ";
    cin>>time;
    
    a_file<<endl<<endl<<intrest<<"% Intrest"<<endl<<endl<<"For "<<time<<" Years"<<endl<<endl;
    for(;;)
    {
    if (counter<time)
    {
    amount = ((intrest/100)*amount)+amount;
    counter++;
    a_file<<"$"<<amount<<" <------Year "<<counter<<endl<<endl;
    
    }
    else
    break;
    }
    cout <<endl<<"press q to quit" << endl; 
        cin >> quit;
    }
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    string undefinde..

    the string is undefined in your program because it is defined in a file called...
    Code:
    #include <string.h>
    or
    Code:
    #include <cstring>  
    using namespace std;
    if u want to read more than one line u could put it into a while loop... for instance..

    Code:
    #include <fstream>
    
    int main()
      {
         ifstream file; // file of type input file stream;
         file.open("filename.txt"); //open filename.txt
         string info;       
    
         file.getline(info, 50); // get first line of text file and store in info;
         cout << info;  //output info to screen.
         while(file.eof() == false)   // while it is not the end of the file.
           {                                    // repeat loop over and over..
              file.getline(info, 50); 
             cout << info;  //output info to screen.
           }
         file.close();   // close the file.
        return 0;
      }
    this is just one way to do it.. there are tons of variations..
    get()
    file >> info
    and the list goes on.. and on.. and on....
    Last edited by tegwin; 11-14-2002 at 12:31 PM.

  9. #9
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    thank you very much. it works now. Where did you learn all this stuff cause im eager to learn more!

  10. #10
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    just one more thing what if i wanted to read only like the 3rd or 4th line?? Or what if i needed to put each of the lines in a different string so i can call them individually???

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    I think you can do like this:

    Code:
    int i;
    for(i=0;!file.eof();i++);
    Now create an array of strings
    Code:
    string *strArray = new string[i];
    And read the file
    Code:
    file.seekg(0);
    for(i=0;!file.eof();i++)
      file.getline(strArray[i], 50);

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, 10: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, 09:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM

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