How do I start reading text file from a cretin string and then stop at another string?
Printable View
How do I start reading text file from a cretin string and then stop at another string?
If you want to find a string in a file, you could transverse through the file until you find what you're looking for. Just use a loop.Code:char foo[50];
fgets(foo, 50, stdin);
printf("Your string was %s", foo);
I am not sure if that will work.
1) I dont know the length of the text file
2) I dont know were the string is located
3)I am using fstream to open the file
fstream is C++
and it's alot easier to find a string.
I don't know if C has a way to get a string up to a delimeter other than reading one character at a time. THere might be, but I don't know C that well.Code:ifstream inFile;
string foo;
inFile.open("file.txt");
do {
getline(inFile, foo, '\n');
} while (foo != "The string I want") // Or whatever you want. You might have to mess
// with the delimeter to get the string you want.
Well that ends it at a string
Now I just need to know how to begin it at a string
it stops reading the text after ; is found now I need to start reading the text when "a" is foundCode:do
{
getline(fin, foo, '\n' );
cout<<foo<<endl;
}
while (foo != ";");
Code:char blah;
string foo, string1;
do {
inFile.get(blah);
} while (blah != 'a');
string1 = blah;
getline(inFile, foo, '\n');
string1 += foo;
So I belive this is the last question If I find the starting string and ending string how would I go about pulling all the data from the middle. the way I was doing it before will not work with the other code.
what I was doing is looking for keywords per line and what erver fallowed I captured but for some reson that is not working now.
Paste your code.
well I thought it was working but I geuss not when I ran another test.
Should this not end when it comes to string BallSpin?Code:
string test,name;
name = "BallSpin";
do {
getline(fin,test);
cout<<test<<endl;
system("PAUSE");
} while (test != name);
also in my text file it is "AnimName BallSpin" so i might have to include the whole line instead of just the Key word "BallSpin"
It should exit the loop when your string says BallSpin, but I noticed you didn't put a delimeter on your getline function. That would by default make getline read until the first \n. Unless your file is simply "BallSpin" it wouldn't work.
Code:getline(fin,test,' ');
getline's delimiter defaults to the newline.
Yeah, I just checked and noticed that. In any event, he more than likely wants a space as the delimeter.
well when I use getline(fin,test,' '); it does not exit
but when I include the whole line and use
getline(fin,test); it will exit now. I can work with that.
so when it exits how do i pickup after were I left off at with out including were it left off? I need there very next line.
edit:
I even tryed '\n' and it still did not work.
Code:#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
string foo, string1;
string test,name,name2,name3;
//BallSpin
int main(int argc, char *argv[])
{ name2 = "AnimName ";
name = "BallSpin";
name3 = name2 + name;
fstream fin("image1.txt");
do {
getline(fin,test,'\n');
cout<<test<<endl;
} while (test != name3);
system("PAUSE");
return EXIT_SUCCESS;
}
Code:AnimName BallExplode
File 1.bmp
Left 1
Right 1
Top 1
Bottom 1
Red 1
Green 1
Blue 1
Delay 1
Width 1
FrameW 1
;
AnimName BallSpin
File null.bmp
Left 0
Right 92
#Top 0
Bottom 0
Red 0
Green 0
Blue 0
Delay 0
Width 0
FrameW 0
;
Ok, first off you're not declaring your variables. Secondly you declared an fstream, you should declare ifstream.
The reason why '\n' or space doesn't work in this file is because if the delimeter was a newline character it'd read in "AnimName BallSpin" if it was a space, it'd read in "Ballspin\nFile"
What you could do is either put a space after each word before you start a new line or you could resize your string after you read in a word so that it would only fit "BallSpin" and nothing more. There are probably numerous other ways to handle this.
so in the text file that is posted how would you pull the data out?