Reading Text from a file

This is a discussion on Reading Text from a file within the C++ Programming forums, part of the General Programming Boards category; im trying to read some text from a file it compiles witn no errors but it doesnt print out whats ...

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    250

    Reading Text from a file

    im trying to read some text from a file it compiles witn no errors but it doesnt print out whats in the txt file
    heres my code
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
        char data [256];
        cout<<"opening file..."<<endl;
        ifstream open_file ("new.txt", ios::app);
        open_file>>data;
        cout<< data <<endl;
        cin.get();
    }

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    ios::app is used when you open a file and write from the end of file.
    In your case, you need ios::in
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Banned
    Join Date
    Oct 2004
    Posts
    250
    ok thanks it works now but ive ran into another problem im trying to use the data i get from the txt file to determine what leval im on but it wont compile
    heres my code
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
        char data [256];
        cout<<"opening file..."<<endl;
        ifstream open_file ("new.txt", ios::in);
        open_file>>data;
        if(data == 1)
        {
            cout<<"you are on leval one"<<endl;
        }
        else if(data == 2)
        {
            cout<<"you are on leval 2"<<endl;
        }        
        cin.get();
    }

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Your char data[256] is known as C-style string. You compare two of these using the function strcmp() http://cppreference.com/stdstring_details.html#strcmp

    And then there's C++-style string declared like this
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string myString = "ABC";
    
        // more intuitive to compare than C-style string
        if ( myString ==  "ABC" )
            cout << "Bingo";
    
        return 0;
    }
    For either string style, it's indicated by opening and closing double quotes (e.g. "H", "123")

    BUT, in your case above, you might want to read data from the file to a variable of type int, though.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Banned
    Join Date
    Oct 2004
    Posts
    250
    thnx again i noticed it reads text from only the top line what could i do to read it from the whole file or a certain line?

  6. #6
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    I'd like to know that too. How do I make it read in the whole txt document, or a specific line?

    Code:
    string data;
    
        cout << "opening file..." << endl;
        ifstream open_file ("new.txt", ios::in);
    
        while(!open_file.eof())
        {
          getline(open_file, data);
          cout << data << endl;
        }
    
        cin.get();
    That makes it read the whole document, but I still can't figure out how to pick out a specific line in the document.
    Last edited by stillwell; 10-15-2004 at 03:58 AM.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    skipping to a certain point in a document is more of a binary thing, but with text you can always do this:

    Code:
    ...
    std::cin.ignore(10000,'\n');  //ignore one line
    ...
    or set up special characters in your files to denote fields, for example, use '$' to denote prices, so you can use this:

    Code:
    ...
    std::cin.ignore(10000,'$');  //ignore everything up to + including the next '$'
    ...
    the result of that file would look something like this:
    Code:
    BanHammer  $120.0  ~CB168
    DellComputer  $50.0  ~DL189
    HomeworkThread  $0.10 ~CB666
    in that file, the first field is the name of the item, then the price, then the catalog number preceded by a '~'
    Last edited by major_small; 10-15-2004 at 06:34 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Banned
    Join Date
    Oct 2004
    Posts
    250
    just so i get everything
    Code:
    getline(open_file, data);
    is copying open_file to data?

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>getline(open_file, data);

    using the stream called open_file, remove all data in the input buffer and put it in (ie, read, copy, whatever you want to call the process) the STL string called data until you either run out of memory or until the first newline char is encountered, whichever comes first. If the terminating char terminates input, remove the terminating char from the input buffer but don't put it into the string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 02:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM

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