Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Don't know character necessary to read file line to string
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
coltson
n00b
n00b


Joined: 15 Oct 2005
Posts: 51

PostPosted: Mon Feb 18, 2013 11:10 pm    Post subject: Don't know character necessary to read file line to string Reply with quote

I am reading a file and want to compare the content from the first line of this file to a certain word. Here is what I am trying to do:

Code:
getline (fileReader,line);
    if (line == "Image")
      cout << "Wellingtton Paulista" << endl;


Here is the beginning of the file
Quote:
Image

/////////////////////////////////////////


As can be seen, my idea is that line is equal to "Image", so the expression is evaluated to true and then cout is executed.
But is not working. I suspected there is a end line character that it is being read and stored together with Image.
However when I put a
Code:
cout << line << endl;
it just prints
Quote:
Image
. But if I put a
Code:
line="Image"
before the if expression it works as intended. So what character I should put in the getline so it just captures the "Image"? I tried
Code:
 getline (fileReader,line,'\n')
and it didn't work.

Thanks for any help
Back to top
View user's profile Send private message
lexflex
Apprentice
Apprentice


Joined: 05 Mar 2006
Posts: 194
Location: the Netherlands

PostPosted: Tue Feb 19, 2013 6:00 am    Post subject: Reply with quote

Hi,

I think you can use wildcards ( someting like *mage* ).

Alex.

PS: Sorry, not bash ( I was just doing something similar in a shellscript...).
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7888
Location: Somewhere over Atlanta, Georgia

PostPosted: Tue Feb 19, 2013 3:26 pm    Post subject: Reply with quote

What's the declaration of the variable "line"? The normal C++ getline function should discard the newline character. For example, the following trivial example behaves as expected:
Code:
#include <iostream>
#include <string>

using namespace std;

int main(void) {
    string              line;

    do {
        getline(cin, line);
        if (line == "John")
            cout << "Hello, " << line << ".\n";
    } while (! cin.eof());

    return 0;
}
- John
_________________
I can confirm that I have received between 0 and 999 National Security Letters.
Back to top
View user's profile Send private message
aderesch
Tux's lil' helper
Tux's lil' helper


Joined: 06 Mar 2010
Posts: 123
Location: Berlin, Germany

PostPosted: Tue Feb 19, 2013 5:56 pm    Post subject: Reply with quote

John R. Graham wrote:
The normal C++ getline function should discard the newline character.

Won't discard \r, though. The file probably has CRLF line endings.

ad
Back to top
View user's profile Send private message
Hu
Watchman
Watchman


Joined: 06 Mar 2007
Posts: 9135

PostPosted: Wed Feb 20, 2013 2:31 am    Post subject: Reply with quote

aderesch wrote:
John R. Graham wrote:
The normal C++ getline function should discard the newline character.

Won't discard \r, though. The file probably has CRLF line endings.
That might be a problem if the input came from Windows, but most Linux editors save files with the proper line endings.
Back to top
View user's profile Send private message
coltson
n00b
n00b


Joined: 15 Oct 2005
Posts: 51

PostPosted: Wed Feb 20, 2013 3:29 am    Post subject: Reply with quote

John R. Graham wrote:
What's the declaration of the variable "line"?


Code:
private:
      std::string line;


I don't know from where the file came from, but I tested with
Code:
getline(cin, line,'\r');

and now it is working as a charm. However, if I create a file from zero, and insert "Image" as the first line it doesn't work.
That makes me to imagine that getline is only looking for '\r' and not for '\r' and '\n' as I would like
So there is a way it can work with both kind of files simultaneously?
Back to top
View user's profile Send private message
aderesch
Tux's lil' helper
Tux's lil' helper


Joined: 06 Mar 2010
Posts: 123
Location: Berlin, Germany

PostPosted: Wed Feb 20, 2013 6:43 am    Post subject: Reply with quote

Hu wrote:
That might be a problem if the input came from Windows, but most Linux editors save files with the proper line endings.

We didn't know anything about the origin of the file. In my experience this is _by far_ the most probable explanation for this kind of problem.

ad
Back to top
View user's profile Send private message
aderesch
Tux's lil' helper
Tux's lil' helper


Joined: 06 Mar 2010
Posts: 123
Location: Berlin, Germany

PostPosted: Wed Feb 20, 2013 6:52 am    Post subject: Reply with quote

coltson wrote:
I don't know from where the file came from, but I tested with
Code:
getline(cin, line,'\r');

and now it is working as a charm. However, if I create a file from zero, and insert "Image" as the first line it doesn't work.
That makes me to imagine that getline is only looking for '\r' and not for '\r' and '\n' as I would like
So there is a way it can work with both kind of files simultaneously?

Of course there is. The question is how generic a solution you want/need.
If \n and \r\n are the only variants to check for (should be sufficient for all Win/Unix text files): use getline, test for \r as the last character and remove if found.
Beware: There are files which have only \r as a line ending, and even \n\r have been seen before. Read the Wikipedia article on "Newline" for details.

ad
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7888
Location: Somewhere over Atlanta, Georgia

PostPosted: Wed Feb 20, 2013 1:45 pm    Post subject: Reply with quote

Why try to make *nix programs accommodate DOS file formats? Just convert the file to proper *nix format before processing it with your program. See app-text/dos2unix.

- John
_________________
I can confirm that I have received between 0 and 999 National Security Letters.
Back to top
View user's profile Send private message
dmitchell
Veteran
Veteran


Joined: 17 May 2003
Posts: 1159
Location: Austin, Texas

PostPosted: Sun Feb 24, 2013 6:04 am    Post subject: Reply with quote

Depending on your application it might be acceptable to check if the line begins with "Image" instead of being equal to "Image".
_________________
Your argument is invalid.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum