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<<i<<" "<< str3<<j<<" "<< str4<<k;
}


fclose(data);
fclose(TestFile);
return 0;
}

Recommended Answers

All 8 Replies

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

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?

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?

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)

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

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!

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?

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...

ok thanks I try it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.