Link to home
Start Free TrialLog in
Avatar of rkcth
rkcth

asked on

File input procedure

I have written a database program, that writes a file so that it can be read by a cgi program. It must also be used by the database program that writes it. It is written in a format, that is easily readable by the cgi quickly, unfortunately, this makes it a bit more difficult for the database to parse the file. Here is an example file, I use c++ comments, to help explain different portions. All important information in the file is preceded by a newline. Here goes. . .
0(55)//0 is the record number, there are 44 records total and the 55 is the line where the next record begins (for the cgi, my database will ignore this)
Page Title//the title my cgi, will display in the window, again my program does not read this
Page specific code//another portion that is not read by my database
Number of internal links//again only for the cgi
{//marks the beginning of where my database program should read into internal links
<LI><A HREF=//ignored by database
http://www.yahoo.com');>//the url is the first piece of data my database reads.
Title</A>//Database must read the title, and ignore the </A>
Description</LI>//database must read description and ignore </LI>
... more links would follow
}//marks the end of internal links
{//marks the beginning of external links
a line of cgi stuff
url//important to program
Title//important for program
Description//important to program
... more links would follow
}

1//next record would begin


So the question is basically, how can I read a file in such a way that I can skip unimportant lines and when I get to an important line, skip everything after a certain point.
If I left anything out please say so. This is the last part my program needs and than its done.
I thank you in advance for all of your help
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

Here are some examples to get you started.

fstream Fil("SomeFileName",ios_base::in);
int RecNum
int RecCnt;
string Title;


Fil >> RecNum; // Read record number
Fil.ignore(1); // Ignore the "{".
Fil >> RecCnt; // Read record  count.
Fil.ignore(numeric_limits<int>::max(),'\n'); // Ignore until the end of the line.
getline(Fil,Title); // Read whole title line.

Let me know if you have any questions.
Avatar of rkcth

ASKER

How can I ignore data after a certain character, with the C++ input classes. I was using the C classes, because the C++ ones seem a bit more difficult. They are so easy for output, perhaps I don't understand them well enough. If I can work it out with your information, I'll be sure to give you the points. I noticed you give information all over the boards and would like to thank you. You have helped me with several other problems if I remember correctly:)
Avatar of rkcth

ASKER

I have not heard of numeric_limits<int>::max() and could not find it in my references
>> the C++ ones seem a bit more difficult.
They might be a little strange at first, but they are much much easier to use.  They have so many powerful features that the C ones done.  Plus they are type safe.

numeric_limits<> is defined in the <limits> include file.  Just include <limits> and you should be fine.  The templates inside define the mathematical ranges and other properties of the basic numeric types.
>> How can I ignore data after a certain character
I guess there are two issues in this.  First you need to read data up to that character, then you read and ignore data from there on up to some other point, probably the end of the line.

Exactly how you do that will depend in the nature of the data to be read, the delimiting character for that data, and delimiting character for the data to be ignored.  But for example, if you want to read all the data on a line up to an "!", then ignore the rest of the data on the line, you could do.

string Data;

getline(Fil,Data,'!'); // Read up to the "!";
Fil.ignore(numeric_limits<int>::max(),'\n'); // Ignore the rest of the line.

If the '!' doesn't always appear on the line you might have to take a different approach, like read the whole line into the string, then search for the "!" in the string and remove it and what follows it if found.

If the data before the "!" is numeric instead of a string, you might use a different technique to read it.  

As you can see there are lots of possiblilities, it depends on the exact details of what you want to do.
Avatar of rkcth

ASKER

The program is done, thank you nietod. I must say, you give the most concise answers. Within a minute or so of reading your answer, I had it figured out. Within 3 hours the entire project was finished.