reading from txt file(plz someone help)

This is a discussion on reading from txt file(plz someone help) within the C++ Programming forums, part of the General Programming Boards category; Hi everyone I need to be able to read each character from a text file and store them in an ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    9

    reading from txt file(plz someone help)

    Hi everyone

    I need to be able to read each character from a text file and store them in an array
    here is the text file:
    *********************
    Ace of Spades
    10 of Spades
    3 of Clubs
    King of Spades
    5 of Spades
    *********************
    Queen of Diamonds
    King of Hearts
    Queen of Hearts
    Queen of Clubs
    Queen of Spades
    *********************
    2 of Clubs
    2 of Hearts
    Ace of Clubs
    2 of Diamonds
    Ace of Diamonds
    *********************
    2 of Hearts
    3 of Hearts
    4 of Hearts
    5 of Hearts
    6 of Hearts

    for instance i need to be able to store the integers in an array.. and then i need to store the string hearts ,clubs in another string array.and i need to ignore the spaces, the'*' so far i have not been able to do it.. i seriously need help. i've been trying different thongs like getline(), indata.getline, getline(indata, str)
    none have been working

    here's what i did..i managed to make the program read the file

    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    using namespace std;

    int main()
    {
    string str;
    ifstream inData;
    string filename;
    cout << "Enter the name of the file containing the hands: " << flush;
    cin >> filename;
    inData.open(filename.c_str( ));


    but now i don't know how to continue. i am really bad at programming.... plz someone help me... thanks

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    9
    when i tried this it's actually outputing something tot he screen..plz plzz someone help..

    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    using namespace std;

    int main()
    {
    char a[25];
    ifstream inData;
    string filename;
    cout << "Enter the name of the file containing the hands: " << flush;


    cin >> filename;

    inData.open(filename.c_str( ));
    inData >> a;
    std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
    cout << a << endl;
    std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );




    }

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    9
    plz someone help me. plz..plzz...plz ( this is what i tried now..but it still is not working. the indata.getline is not working.in fact the screen just disappears when i enter the filename

    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    using namespace std;

    int main()
    {
    int maxLength=100;
    char c;
    char line[maxLength];
    ifstream inData;
    string filename;
    cout << "Enter the name of the file containing the hands: " << flush;


    cin >> filename;


    inData.open(filename.c_str( ));

    inData.getline(line,maxLength);
    cout << line << endl;


    std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
    std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
    cin.get(c);
    cout << c << endl;
    std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
    std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );



    return 0;

    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Maybe you could learn about code tags whilst waiting patiently for an answer.
    See the intro threads at the top of each forum.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    9
    sorry.. i don't understand....

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    This link
    << !! Posting Code? Read this First !! >>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    9
    this is what i tried next. but the screen just disappears.. plz salem will u be kind enough to help me... i just need to prevent the output window from disappearing everytime... i ahve tried everything..it's not working



    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    using namespace std;

    int main()
    {
    int maxLength=100;
    char c;
    char line[maxLength];
    ifstream inData;
    string filename;
    cout << "Enter the name of the file containing the hands: " << flush;


    cin >> filename;


    inData.open(filename.c_str( ));

    inData.getline(line,maxLength);
    cout << line << endl;




    cin.get();
    cin.get();



    return 0;

    }

  8. #8
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    first of all if you are trying to read the entire file why aren't you using a loop?

    Input/Output with files

    second you have alot more information posted that the file contains than you have actual variables to save it to.

    third

    the value of patience - Google Search

    fourth

    << !! Posting Code? Read this First !! >>

    If you can't learn to be patient and respectful to professionals in the field, how could you even expect to find the correct answer to your problem? Or even lead a partially successful life with such lack of people skills.

    sorry.. i don't understand....
    Demonstrates a lack of ability to learn from other's input... you are treading on dangerous territory with the experienced board members here, and trust me if you intend to program long burning a bridge with some of the most insightful programming minds I've met wouldn't seem like a prudent decision.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Well that was decidely pointless...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble reading from txt file
    By ColtsBR in forum C++ Programming
    Replies: 1
    Last Post: 09-17-2010, 11:23 PM
  2. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 05:41 PM
  3. reading file word by word
    By 98holb in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 04:49 PM
  4. Help with getline() and file reading
    By evilkillerfiggi in forum C++ Programming
    Replies: 5
    Last Post: 10-15-2005, 03:49 PM
  5. Reading a line from a txt file
    By Nippashish in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2002, 05:22 PM

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