Read Text File - first line has extra char
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Read Text File - first line has extra char

  1. #1
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    Read Text File - first line has extra char

    This should be a trivial excersize, but for some reason I'm getting a problem
    when I read a text file. I'm reading a list of strings in text file, with one string per line. The first line has extra characters in the string, the rest of the lines read are fine, and I can't understand where the extra characters come from. Any ideas?

    The file format is this...
    A
    AA
    AAN
    AAP
    AAPL
    AAWW
    AAXJ
    ....
    ...


    Code:
    std::vector<std::string> strSymbolList;
    
    std::string s = "";
    std::ifstream infile( m_strFileSymbols );
    while( std::getline(infile, s) )
    {
         strSymbolList.push_back(s);
    }
    infile.close();

  2. #2
    Join Date
    May 2012
    Location
    Bonn, Germany
    Posts
    43

    Re: Read Text File - first line has extra char

    Is your text file UTF-8-encoded?
    If yes, the first character is probably the Byte Order Mark.
    Read more about it here: http://en.wikipedia.org/wiki/Byte_order_mark

    If you want to remove it, save the text file explicitly as ANSI.

  3. #3
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    Re: Read Text File - first line has extra char

    Quote Originally Posted by TomasRiker View Post
    Is your text file UTF-8-encoded?
    If yes, the first character is probably the Byte Order Mark.
    Read more about it here: http://en.wikipedia.org/wiki/Byte_order_mark

    If you want to remove it, save the text file explicitly as ANSI.
    Yes that did it. The file was exported from MSSQL and that was the problem.

    Thanks.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Read Text File - first line has extra char

    Quote Originally Posted by cbpetro View Post
    This should be a trivial excersize, but for some reason I'm getting a problem
    when I read a text file. I'm reading a list of strings in text file, with one string per line. The first line has extra characters in the string, the rest of the lines read are fine, and I can't understand where the extra characters come from. Any ideas?
    It would help if you attached the actual file you're reading instead of typing in what you think is in the file. The character may be a non-visible control character or some other character that you do not expect.

    Regards,

    Paul McKenzie

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center