Read in from a text file

Hello!

I have a textfile with ID numbers and text. The ID-numbers is exclusively on its on row, with text under it. Example:

1098290-7
here is text

I wrote this code, in order to find the specifik row number for the ID:

char row[MAX_SIZE];

while (file.getline(row,MAX_SIZE))
{
rowNR ++;

if (row == "1098290-7"
)
{
cout << "\n" << rowNR;
}

Why isn't it working? The program can't find the string at all. But if I just write "cout << row", then the program find everything in the file, including a row with 1098290-7.

This:
if (row == "1098290-7")
will not behave as you would expect.

You need to use
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

Better yet, just make "row" a std::string, so you could just do
1
2
3
4
5
6
string row;
while (getline(file, row))
{
    rowNR++;
    if (row == "1098290-7") // This works with std::string but not char[]
        // ... 
Topic archived. No new replies allowed.