I need some help reading in data with fstream

This is a discussion on I need some help reading in data with fstream within the C++ Programming forums, part of the General Programming Boards category; Ok suppose I file a file called bob.txt somewhere, and my program can open it and access it and theres ...

  1. #1
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116

    I need some help reading in data with fstream

    Ok suppose I file a file called bob.txt somewhere, and my program can open it and access it and theres no errors and all that good stuff. Now the contents of that file look something like this:
    sjf73#kdn&752d
    abc432xx&7
    That (hopefully) won't make sense to anyone, but that's ok. I need to read in both lines, and store them in seperate variables, but I have no idea how to do that. Can anyone help me?
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Code:
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
        ifstream input("bob.txt");
        string str1, str2;
        getline(input,str1);
        getline(input,str2);
        input.close();
        return 0;
    }
    After this, str1 will hold sjf73#kdn&752d and str2 will hold abc432xx&7.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble Reading Data from Files
    By CConfusion in forum C Programming
    Replies: 11
    Last Post: 04-06-2006, 07:12 PM
  2. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  3. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  4. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  5. Help reading data file...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-25-2002, 11:49 AM

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