954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

not sure how to save fscanf to file

I have a script to read a file that has text on lines. The script is supposed to take the text and make them into space delimited and save the new text to a new file. I am able to only accomplish this with 1 line from the text file. I am new to c++, and have been using Herbert Schildt's C++ book. My other problem I am trying to solve is that the file I am using I wont know how many lines of text I will be converting. I have tried different loops none seem to work. here is a sample of what I've written if any one has any ideals, I would really appreciate the assistance. This is not a test for class or anything:

int main(void)
{
FILE *data;
FILE *TestFile;
// FILE *dataholder;

int i;
int j;
int k;
int x = 0;
char ch;
char str[4], str2[6], str3[6], str4[6];

if((data=fopen("file.txt", "r")) == NULL) {
printf("cannot open file.\n");
}

if((TestFile=fopen("testing.txt", "w")) ==NULL) {
printf("cannot open file.\n");
}



{
fscanf(data, "%[DAT]%[C]%d%[T]%d%[F]%d" , str, str2, &i, str3, &j, str4, &k);
cout << str<<" "<< str2<

cujo401
Newbie Poster
4 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

Hello,

I do not remember the exact syntax, but you need to loop until you reach the end of file. Otherwise your program just makes one iteration.

Christian

kc0arf
Posting Virtuoso
Team Colleague
1,937 posts since Mar 2004
Reputation Points: 121
Solved Threads: 57
 

I guess you want something like this:

...
while (!feof (data))
{
fscanf(data, "%s%s%d%s%d%s%d" , str, str2, &i, str3, &j, str4, &k);
fprintf (TestFile, "%s\t%s\t%d\t%s\t%d\t%s\t%d\n", str, str2, i, str3, j, str4, k);
}
...

Anyway, why are you mixing C and C++ instructions?

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

I guess you want something like this:

...
while (!feof (data))
{
fscanf(data, "%s%s%d%s%d%s%d" , str, str2, &i, str3, &j, str4, &k);
fprintf (TestFile, "%s\t%s\t%d\t%s\t%d\t%s\t%d\n", str, str2, i, str3, j, str4, k);
}
...

Anyway, why are you mixing C and C++ instructions?


yeah, I actually rewrote it all in C++ here is the new script, in this script the file sample2.txt, I manually delimited with spaces but will not be able to do in the final process. Where sample2 is the file resolve will be used. Any ideals for this?

Attachments knowledge.cpp (0.66KB) sample2.txt (1.27KB) resolve.txt (1.15KB)
cujo401
Newbie Poster
4 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

Let's see if I get it right: you get a txt file, that contains data in a xxxyyyzzz format, and you want to write it in a file, in a format like xxx yyy zzz ?
If so, and if your data has some particular characteristics (I saw some ADT, Pxx and Dyyy in saple2.txt file), I would use some string functions (strtok, for example)

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

have you not tried the ANSI c++ string class (using )?

Then you could add the space quite easily:

string str;

str += " "; // add the space, you will need to get the data into str first but ill leave that up to you!
1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

I am very new to c++ and don't know that structure (strtok), but to answer your question, that is what I am trying to accomplish (get a txt file, that contains data in a xxxyyyzzz format, and write it in a file, in a format like xxx yyy zzz). I also want the new file contents to be displayed. That format ADT Pxx Dyyy Annn, one other thing is that the variables of P D and A may change in length, will this make a difference in how I set the array length?

cujo401
Newbie Poster
4 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 
one other thing is that the variables of P D and A may change in length


That's why I am talking about strtok :). It's a string manipulation function; it seems to me to be the simplest solution for what you want...
As long as your variables change in lenght, another solution can be this: read the fiel char by char; first 3 chars will be the ADT; write them to the output file; if a char = P, then here begin the 2nd variable; write every char to the output file; read chars until you read a D, here begin the 3rd variable, etc...

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

ok thanks I try it

cujo401
Newbie Poster
4 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You