-
March 21st, 2007, 04:00 PM
#1
cstdiofile::readstring question
Hello all
I have 2 txt files: text.txt, and text2.txt.
Text.txt contains this:
Code:
ddddsd, 1111, sdsdss, 22222
ddddsd, 1111, sdsdss, 22222
ddddsd, 1111, sdsdss, 22222
and text2.txt is an empty file.
What I want is to read only the numbers from the first file and write them into the second file. How can i achieve this? What Ive written so far works, except that it takes all the contents of the first file and places it into the 2nd (i only need the numbers though).
Code:
#include <afx.h>
using namespace std;
int main()
{
CStdioFile fisierRead, fisierWrite;
CString s;
fisierRead.Open(_T("C:\\text.txt"), CFile::modeRead|CFile::typeText);
fisierWrite.Open(_T("C:\\text2.txt"), CFile::modeWrite|CFile::modeCreate);
while(fisierRead.ReadString(s))
{
fisierWrite.WriteString(s+ L"\n");
}
return 0;
}
so how can i only read the numbers?
thank u
PS - i dont need code only a hint
the doer alone learneth. (nietzsche)
-
March 21st, 2007, 04:37 PM
#2
Re: cstdiofile::readstring question
What you have to do is apply a bit of logic in your code before you write to another file.
That is , you need to seperate your strings . Use Tokenizer for that,
have a look at strtok in MSDN , and as per your line , you need to read your second and fourth token only. Easy huh!!!!! :-)
"Dont Forget to rate if it helped"
-
March 21st, 2007, 05:04 PM
#3
Re: cstdiofile::readstring question
ok thank you very much
the doer alone learneth. (nietzsche)
-
March 22nd, 2007, 12:06 PM
#4
Re: cstdiofile::readstring question
uhm, how can i read a certain token (like you say, i need to read the 2nd and 4th token).
Ive managed to read all tokens now so now its like this:
Code:
#include <afx.h>
using namespace std;
int main()
{
CStdioFile fisierRead, fisierWrite;
TCHAR buf[100], *token;
fisierRead.Open(_T("C:\\text.txt"), CFile::modeRead|CFile::typeText);
fisierWrite.Open(_T("C:\\text2.txt"), CFile::modeWrite|CFile::modeCreate);
while(fisierRead.ReadString(buf, 99))
{
token= wcstok(buf, _T(" ,"));
fisierWrite.WriteString(token);
while(token !=NULL)
{
fisierWrite.WriteString(token);
token = wcstok(NULL, _T(" ,"));
}
}
return 0;
}
so how can i read only the 2nd for example? It now reads all of them and writes to the file. but i only need 2nd and 4th.
the doer alone learneth. (nietzsche)
-
March 22nd, 2007, 12:19 PM
#5
Re: cstdiofile::readstring question
As you get the tokens, keep track of the token number, and only do
a write for the token numbers that you want.
-
March 22nd, 2007, 03:15 PM
#6
Re: cstdiofile::readstring question
You should probably post here instead of sending a private message ...
that way if others have the same problem they can find the answer.
1) You create a variable which keeps count of the token count ....
Code:
while(fisierRead.ReadString(buf, 99))
{
token= wcstok(buf, _T(" ,"));
int token_count = 1; // above line got the first token
while(token !=NULL)
{
token = wcstok(NULL, _T(" ,"));
++token_count;
if (token_count==2 || token_count==4)
{
fisierWrite.WriteString(token);
}
if (token_count == 2)
{
fisierWrite.WriteString(_T(", "));
}
}
}
2) Another method would to use standard C++ streams instead. The
reading itself can do simple parsing, resulting in code that reads
more like what you are trying to do.
Code:
#include <fstream>
char buffer[100];
std::ifstream in("c:/text.txt");
std::ofstream out("c:/text2.txt");
while (in.getline(buffer,100,',')) // while more first tokens
{
in.getline(buffer,100,','); // second token
out << buffer << ", ";
in.getline(buffer,100,','); // third token
in.getline(buffer,100); // fourth token
out << buffer << "\n";
}
-
March 22nd, 2007, 03:23 PM
#7
Re: cstdiofile::readstring question
i apologyze for sending the pm and thank you for your answer. i cant believe how simple the answer was, all i had to do was to make a variable myself and increment it...
the doer alone learneth. (nietzsche)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|