CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2010
    Posts
    10

    txt file info to array

    hi, wondering if someone could provide a snippet that takes info from a txt file & puts it into an array. I need to use cin.get or stream ins.get (over similar simple means); each line has two pieces of data (numbers) separated by a space. So I'm thinking that I need to be able to use a space delimiter. I don't have problems w/ opening file or extracting info. It's just the space delimiter & trying to use cin.get. Because I have to get one char at a time until I hit a whitespace, maybe I should count the char until a whitespace is encountered, then transfer 0-num characters to an array.

    I know this can be done w/ printf, but I don't think I'm permitted to use this.

    Thnx for any help. I have to work today. So will not be able to provide feedback until later.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: txt file info to array

    You should not use get. Just use operator >>

    Code:
    int v1 , v2;
    
    cin >> v1 >> v2;  // or your ifstream object

  3. #3
    Join Date
    Oct 2010
    Posts
    10

    Re: txt file info to array

    ok, I think "or your ifstream object" is this meant to say "of"? If so, I think I got it. The cin operator will stop at the whitespace as default? & assign my two pieces of data to v1 & v2, then I can put those into my array.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: txt file info to array

    If you are reading from a file, you would normally use ifstream, not cin. Unless
    you are re-directing input.

    Yes, operator >> stops at whitespaces (or illegal character or EOF)

  5. #5
    Join Date
    Apr 2011
    Posts
    9

    Re: txt file info to array

    Quote Originally Posted by Muffalufagus View Post
    hi, wondering if someone could provide a snippet that takes info from a txt file & puts it into an array. I need to use cin.get or stream ins.get (over similar simple means); each line has two pieces of data (numbers) separated by a space. So I'm thinking that I need to be able to use a space delimiter. I don't have problems w/ opening file or extracting info. It's just the space delimiter & trying to use cin.get. Because I have to get one char at a time until I hit a whitespace, maybe I should count the char until a whitespace is encountered, then transfer 0-num characters to an array.

    I know this can be done w/ printf, but I don't think I'm permitted to use this.

    Thnx for any help. I have to work today. So will not be able to provide feedback until later.
    cin will stop at white space. and generally, you can parse each line like
    Use ifstream to read file. like
    ifstream in("filename");
    string line,sub;

    while(getline(in,line))
    {
    /* use line.find_first_of(DELIMINATORS_YOU_USE) to find the locations of the deliminators
    use the locations and line.substr to get the substring
    like sub=line.substr(STARTPOINT,NUMBER_OF_CHARS)
    use atoi or atof to change to number, like atoi(sub.c_str())
    */
    }

  6. #6
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: txt file info to array

    Quote Originally Posted by talentspsp View Post
    cin will stop at white space. and generally, you can parse each line like
    Use ifstream to read file. like
    ifstream in("filename");
    string line,sub;

    while(getline(in,line))
    {
    /* use line.find_first_of(DELIMINATORS_YOU_USE) to find the locations of the deliminators
    use the locations and line.substr to get the substring
    like sub=line.substr(STARTPOINT,NUMBER_OF_CHARS)
    use atoi or atof to change to number, like atoi(sub.c_str())
    */
    }
    Personally I would stuff the line into an istringstream and parse it from there or use boosts lexical_cast. If you must recommend C functions then at least choose the right option. atoi and atof are better replaced by strtol and strtod.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured