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?
Well if you're only writing this for that file, you could read in until a space and read in until a newline each time through the loop, after a few iterations, you'll end up with BallSpin in the string.
Code:do {
getline(fin,test, ' ');
getline(fin,test, '\n');
cout<<test<<endl;
} while (test != name);
That is true but I am trying to start the read at ballspin not to get ballspin.
I am may have been unclear to begin with because I am unclear on what is need to solve the problem and what the problem really is in the end. I thought it would be easy to find ballspin start reading the stop at ;. but I can see now it is not so easy.
I am very appreciative for your help. I know with your help I will be able to solve this problem.
There is no basic way to find something in a stream without having been reading from it already. But regardless, once you find the word ballspin, exit the loop, then start another loop that just keeps concatinating everything onto that same string until you get to the semicolon. That's about the easiest way to do it.
whould I do something like what is shown below?
Code:
do{
getline(fin,test, ' ');
getline(fin,test, '\n');
cout<<test<<endl;
}while (test !=name);
test += blah;
do{
getline(fin,blah, ' ');
getline(fin,blah, '\n');
}while (blah !=";");
More like this:
Code:do{
getline(fin, test, ' ');
getline(fin, test, '\n');
}while (test !=name);
do{
getline(fin, blah, '\n');
test += '\n' + blah; // Don't forget getline() doesn't read in the delimeter
}while (blah !=";");
cout << test << endl;
only one issue if I cahnge it to BallExplode then it does not do any thing? is there something I have to put in so it will read it sence it is at the top?
Edit:
never mind I just have to be carefull with the spaces.
Right now I am going to extract the data and if it works then I am all dont all thanks to you and your greate wisdom.
Edit2:
So how do I take string test and take out the individuel strings from it?
can any one else give me some ideas?
I didn't follow the entire thread, so this may not be what you are looking for, but to take the individual strings out of an existing string, use a stringstream from <sstream>. It works just like an iostream or a file stream, so you can use getline or operator>> on it.