-1

In my program I am supposed to read a text file in line by line and search for the longest palindrome and return its line number.

Each text file is 100,000 lines long with a max length of 15.

With my code, I am able to read each line into

char lines[100000][15]

Except for the blank lines which throw off my calculation of which line contains the longest palindrome.

For example a file containing: (0: is line 0, 1: is line 1, ect.)

0: hello
1: bob
2: joe
3: 
4: cat

Comes up as:

0: hello
1: bob
2: joe
3: cat
4: (whatever 5: would be)

Here's my code for reading the file:

std::ifstream theFile;
theFile.open(argv[1]);

char lines[100000][15];
for (int i = 0; i < 100000; i++)
{
    for (int j = 0; j < 15; j++)
    {
        lines[i][j] = '\0'; //I do this to initialize each char to null
    }
}

while (!theFile.eof())
{
    for (int i = 0; i < 100000; i++)
    {
        theFile >> lines[i];
    }
}

I'm assuming the issue is with the line:

theFile >> lines[i];

not copying over newlines or other formatting characters, but I'm not sure how to get around this so any help would be appreciated.

I have to use an array of char arrays, btw, because I am using MPI to pass the data and I can only send chars and not arrays/strings.

schwingms
  • 264
  • 1
  • 2
  • 8

4 Answers4

1

Try the getline function instead (and fix the loop) like this

 for (int i = 0; theFile.getline ( lines[i],14) && i < 100000; i++)
    ;
Soren
  • 14,402
  • 4
  • 41
  • 67
  • 1
    Please read [Why using EOF is wrong](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). If you are going to recommend a file reading loop, do it correctly. – Thomas Matthews Feb 18 '16 at 00:13
0

getline has a version where you can specify the delimiter. The default version will bump out when it encounters a new line so don't use that.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
pcodex
  • 1,812
  • 15
  • 16
0

You don't need to keep all the strings in memory. Just read one line at a time. If the line is a palindrome and it's longer than the longest one you've seen so far, store it's length and the line number. Then bump the line counter. Something like this:

long line_number = 0;
int longest_length = -1;
long longest_number = -1;
std::string line;
while (theFile.getline(line)) {
    if (is_palindrome(line) && longest_length < line.length()) {
        longest_length = line.length();
        longest_line = line_number;
    }
    ++line_number;
}
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
0

I gave up on trying to put them directly into the array of char arrays. I did this instead, using getline and vector.

std::ifstream theFile; theFile.open(argv[1]);

std::vector<std::string> Lines;
std::string workString;
while(std::getline(theFile,workString))
{
    Lines.push_back(workString);
    workString.clear();
}
theFile.close();

char lines[100000][15];
for (int i = 0; i < 100000; i++)
{
    for (int j = 0; j < 15; j++)
    {
        lines[i][j] = '\0';
    }
}


for (int i = 0; i < 100000; i++)
{
    for (int j = 0; j < Lines[i].size(); j++)
    {
        lines[i][j] = Lines[i][j];
    }
}
schwingms
  • 264
  • 1
  • 2
  • 8