| View previous topic :: View next topic |
| Author |
Message |
coltson n00b

Joined: 15 Oct 2005 Posts: 46
|
Posted: Mon Feb 18, 2013 11:10 pm Post subject: Don't know character necessary to read file line to string |
|
|
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 . But if I put a 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 |
|
 |
lexflex Tux's lil' helper

Joined: 05 Mar 2006 Posts: 124 Location: the Netherlands
|
Posted: Tue Feb 19, 2013 6:00 am Post subject: |
|
|
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 |
|
 |
John R. Graham Administrator


Joined: 08 Mar 2005 Posts: 6447 Location: Somewhere over Atlanta, Georgia
|
Posted: Tue Feb 19, 2013 3:26 pm Post subject: |
|
|
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 _________________ This space intentionally left blank. |
|
| Back to top |
|
 |
aderesch Tux's lil' helper

Joined: 06 Mar 2010 Posts: 122 Location: Berlin, Germany
|
Posted: Tue Feb 19, 2013 5:56 pm Post subject: |
|
|
| 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 |
|
 |
Hu Watchman

Joined: 06 Mar 2007 Posts: 7621
|
Posted: Wed Feb 20, 2013 2:31 am Post subject: |
|
|
| 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 |
|
 |
coltson n00b

Joined: 15 Oct 2005 Posts: 46
|
Posted: Wed Feb 20, 2013 3:29 am Post subject: |
|
|
| 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 |
|
 |
aderesch Tux's lil' helper

Joined: 06 Mar 2010 Posts: 122 Location: Berlin, Germany
|
Posted: Wed Feb 20, 2013 6:43 am Post subject: |
|
|
| 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 |
|
 |
aderesch Tux's lil' helper

Joined: 06 Mar 2010 Posts: 122 Location: Berlin, Germany
|
Posted: Wed Feb 20, 2013 6:52 am Post subject: |
|
|
| 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 |
|
 |
John R. Graham Administrator


Joined: 08 Mar 2005 Posts: 6447 Location: Somewhere over Atlanta, Georgia
|
Posted: Wed Feb 20, 2013 1:45 pm Post subject: |
|
|
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 _________________ This space intentionally left blank. |
|
| Back to top |
|
 |
dmitchell Veteran


Joined: 17 May 2003 Posts: 1154 Location: Austin, Texas
|
Posted: Sun Feb 24, 2013 6:04 am Post subject: |
|
|
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 |
|
 |
|