Hi all.

Before you all scream "search the forum", i already have, this and many others. cant seem to find the problem with my code.

Basically, i have a fairly large text file, each line consisting of 20 fields seperated by a `. i have got my program to split each line into an array (with a for loop), thats all fine.

Problemo starts when i want it to compare one of these fields (first one FYI) with a variable (char/std::string), and if it does not match, move to the next line of the file and so on.

more or less search the first field of the file for one particular string.

here is the code. (excuse us if i mess up the code /code thingy)

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>

using namespace std;

int main ()
{

    char chrLine[1024];
    char buffer[10000];
    char *arrLine[20];
    int iii = 2;
    int bbb = 1;

    fstream fsFile ("/home/kevin/PlantBatchRecord.txt");

    while (fsFile.getline(chrLine,1024))
    {

        //fsFile.getline(chrLine,1024);

        arrLine[1] = strtok(chrLine,"`");

        cout << arrLine[1] << endl;

        for(iii; iii<21;iii++)
        {
            arrLine[iii]= strtok(NULL,"`");
            cout << arrLine[iii] << endl;
        }
        //    cout << chrLine<< endl;
        fsFile.getline(chrLine,1024);
    }

    fsFile.close();

    return 0;
}

and heres a sample of the file its analysing:

-2136142810`DIGOD`1912181208`7/1/2009 0:00:00`14.00`34.00`476.00``6`30/12/1899 16:56:40`30/12/1899 15:26:58`2`0`0````0`{092DED01-4B2D-482F-953C-55AC25F5EEB2}`

-2135193690`LOAFFC`5453180308`20/3/2008 0:00:00`15.00`34.00`510.00``2`30/12/1899 16:40:15`30/12/1899 13:36:36`3`0`0````36`{9342CD21-0093-475F-8BA5-A6F96D69E55E}`

-2135193319`PHWINR`3522080708`15/7/2008 0:00:00`11.00`6.00`66.00``2`30/12/1899 16:04:34`30/12/1899 15:41:05`1`0`0````0`{20EE4310-5E16-4390-A32E-855CDDEF5B97}`

-2135134166`CTMAC`1502230408`2/5/2008 0:00:00`3.00`34.00`102.00`#7`0`30/12/1899 11:37:15`30/12/1899 8:57:21`2`0`0``0``0`{4CAA0FDB-D8A8-4875-93FF-31131FF40CA5}`

-2133567660`ECDICP`2262040408`7/4/2008 0:00:00`12.00`34.00`408.00``5`30/12/1899 16:24:40`30/12/1899 15:47:23`2`0`0````49`{174DFC95-202F-4060-9CC6-8ED7F87D8674}`

-2133511239`KABENFJ`4601060409`7/4/2009 0:00:00`22.00`34.00`748.00``6`30/12/1899 11:23:52`30/12/1899 9:29:22`2`0`0````0`{F7400055-E446-4A7F-946D-53F00041B633}`

-2133402358`GRCHER`3412131008`22/10/2008 0:00:00`32.00`34.00`1088.00``6`30/12/1899 13:01:12`30/12/1899 9:55:56`2`0`0````0`{017DA154-DCC4-4B17-9701-BA0CC14BBE66}`

ive tried many things, including puting a fsFile.get() at the end of the while loop, changing the while loop to a for or do while loop, getting rid of the cout statements within loop, everything i can think of, or thats been suggested in similar threads here and elsewhere.


Thanks all in advance for your help, or at least for reading my post =].

mvmalderen commented: Code tags on first post and someone who did an effort first :) +14

Recommended Answers

All 8 Replies

On Line 35

fsFile.getline(chrLine,1024);

This will cause you to skip every alternate line of information.

Second error,

char *arrLine[20];

the array is of 20 elements so they would be placed "0,1......18,19".
So The code in the for loop would cause an out-of-bound error.

Problemo starts when i want it to compare one of these fields (first one FYI) with a variable (char/std::string), and if it does not match, move to the next line of the file and so on

Apart from that you are not using any checking mechanism to see for a match.

On Line 35

fsFile.getline(chrLine,1024);

This will cause you to skip every alternate line of information.

Second error,

char *arrLine[20];

the array is of 20 elements so they would be placed "0,1......18,19".
So The code in the for loop would cause an out-of-bound error.


Apart from that you are not using any checking mechanism to see for a match.

Sorry, forgot to mention that i had omitted the way checking for a match, it made no difference to the actual problem. (for clarity)

About the array, in the for loop it seem to work fine, behaves as i want it to. the problem is in the while loop, only ever analyzing the first line. just in case, how would i fix this in case this is the prob. (probably is, you know more than me). =]

and thirdly, removed the extra getline, still the same prob.=[


Thanks for the help mate.

Use a do while loop instead of the while loop and then you can add the getline inside the loop ,

do {
//getline here.
//other code here.
}while (!fsFile.eof());

So you read the file until the end,

Use a do while loop instead of the while loop and then you can add the getline inside the loop ,

do {
//getline here.
//other code here.
}while (!fsFile.eof());

So you read the file until the end,

Sorry... still no luck. ive even made a simple file, with three fields, same delimiter, same everthing. still READS FIRST LINE ONLY. everthing else is as expected. do i need to use something like seekg to make sure that it starts from the start??
im 99% sure it already is tho.

anyway, its late here in oz, im off, ill deal with it tomorow. thanks for your help.

Cheers
Kev

Are you wedded to using strtok()? If not, use the '`' char as the delimeter of the parsing phase, whether the parsing is done initially or after first line read.

while(read in first line)
{
    parse first token of first line
    if(first token == some arbitrary token)  
      parse rest of first line
      read in second line
      parse second line
      read in third line
      parse third line
}

Parsing line without using strtok() could be done with stringstream

stringstream ss(line to parse);
while(getline(ss, token, '`'))
   store token in array;

token will be an STL string, which can be stored as a C style string by using the .c_str() method if you want.

To use the stringstream class you need to include the sstring (or something like that) header file.

I note that you have serial '`' characters in the array, meaning some fields will be empty strings that you will have to deal with somehow, particularly if you want to ignore them.

If you use the return value of eof() to terminate the loop you are at risk of crashing your program by doing the loop one time too many, since eof() will return false after tyring to read beyond EOF, not when it reads EOF. Even if using as you do works in this program, I can guarantee it won't in another program sometime, someplace, and you will be frustrated trying to figure it out. So don't do it.

In your code, when iii equals 20 in the loop you use, it will overwrite the array since arrLine[20] is out of bounds as there are only 20 elements in arrLine making the last valid index 19. See post by Sky Diploma.

Hi all again. With a bit of tinkering, and removing a few bits of code, ive got it to cycle through all the first fields in the file, at the moment forgetting the rest of the fields. but im having problems with the comparason if statement. im comparing a char * var with a char * [].

heres the code.

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>

using namespace std;

int main ()
{

    char chrLine[1024];
    char buffer[10000];
    char *arrLine[22];
    int iii = 2;
    int bbb = 1;
    int intLine = 0;
    bool blnGotLine = false;
    char *chrLineID;
    fstream fsFile ("/home/kevin/PlantBatchRecord.txt");
    //cout<<"Enter ID:" <<endl;
    chrLineID = "2098342466";
    fsFile.seekg(ios::beg);

    while(blnGotLine == false)
    {

        fsFile.getline(chrLine,1024);

        arrLine[1] = strtok(chrLine,"`");

        if (arrLine[1] == chrLineID)
        {
            blnGotLine = true;
            intLine = bbb;
        }

        cout << arrLine[1];// << endl;


        bbb++;
    }
    fsFile.close();
    //cout << arrLine[3]<< endl;
    return 0;
}

thanks for the help all

Hi all,

Just letting you know i figured out my last question. you gotta use strcmp for future reference. ill post program as snippet when im done, and link to thread for future reference. thanks all for your help.

Kev

For future reference -

use the wxTextFile class in the wxWidgets libraries.

much easier.

just google wxTextFile

two "gotchas":

use wxTextFile.Open() to open a file, wxTextFile(wxString filename) overwrites the file. (same as wxTextFile.Create())

make sure you use wxTextFile.Write() at the end or nothing happens.

cheers all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.