Advertisement

Text File I/O

Started by May 25, 2000 08:52 AM
4 comments, last by Run_The_Shadows 24 years, 7 months ago
Well its been a while since i asked for help again on this topic which means i''ve finally hit another bump. I can''t seem to figure out how to do a couple of things with file I/O. 1.)Search a ofstream for a specific string, in my case a number followed by a colon. 2.)Be able to delete the entire line after that string. Thnx, -Run_The_Shadows -Run_The_Shadows@excite.com
If you use a nice, normal FILE, this code should do what you want (not tested though). This is my interpretation of what you want anyway... Please forgive my parsing functions... I wrote them myself and I will give you the header if you want it (you would need it to compile this).
FILE *InFile = fopen("input.txt", "r");FILE *OutFile = fopen("output.txt", "w");char Buffer[1024]; // a 1Kb buffer for those really long lines bool EraseLine = false;while (fgets(Buffer, 1023, InFile)){if (EraseLine == true){EraseLine = false;continue;}if (atoi(Parse(Buffer, 1, '':'')) == SomeNumber)EraseLine = true;fputs(Buffer, OutFile);} 

Something like that anyway... I may have screwed up the params on fputs().

--------------------


You are not a real programmer until you end all your sentences with semicolons;

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Advertisement
Thnx for answering, but will that work in a non-Visual environment? I use VC++ to program this, but since i am not exactly proficient in it, i am still coding without MFC.


-Run_The_Shadows
-Run_The_Shadows@excite.com
The FILE stuff is all basic C stuff, so it should work (assuming the logic is correct) on any C/C++ compiler. I believe it''s all in the StdIO header.
What yo ucan do is run a loop that loops for a certain word

open the file then make a buffer that reads each word and then make tags in the file so in your file have some thing like this

TAG1
the is all the tag one stuff

then in the program go
while(strcmpi(buffer,"TAG1")!=-0)
move the buffer to the next word or character

get it? I may beable to explain it a little better in class

------------------------------------------------------------
"Theres a guy in my programming class that is not gay, its not me, and his name starts with an R"
-Brendon Glanzer
said tomarrow
How can you search an ofstream? That is something you are writing to disk. I think you mean an ifstream. How about reading the whole thing into a stringstream (using something like "stringstream strstrm << file.rdbuf()"), and analysing the string? (Which you can get from "strstrm.str()")

Part 2 is harder, though. I suggest reading the file in a line at a time, putting the lines into a list. Then search all the lines for what you want, delete the offending line, and write out the results.

This topic is closed to new replies.

Advertisement