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;
}

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?

Attachments
#include <iostream.h>
#include <fstream.h>
#include <string.h>

int main ()
{
    char code[10];
    char prcode[10];
    char dcode[10];
    char artid[10];
    int x = 0;    
    ifstream knowledgeData;
      
    knowledgeData.open("sample2.txt",ios::in);//the file sample2.txt is my manual upgraded file this will need to be changed with resolve.txt        
    for (x = 1; x <= 71; x = x + 1)
    {
	knowledgeData >> code >> prcode >> dcode >> artid;
	cout << "ADT: " << code <<" " << "Primary Code: " << prcode << " " << "Device Code: " << dcode << " " << "Article ID: " << artid <<endl;     	
	
    }
    knowledgeData.close();
    return 0;
}
ADTP8D189A845
ADTP3D15A923
ADTP9D193A561
ADTP10D52A815
ADTP9D201A454-
ADTP10D185A615
ADTP9D191A429
ADTP10D175A547
ADTP47D204A917
ADTP47D214A695
ADTP10D206A886
ADTP47D137A 
ADTP10D52A525
ADTP47D144A860
ADTP10D206A480
ADTP10D185A528
ADTP10D52A631
ADTP10D52A631
ADTP10D52A631
ADTP4D180A630
ADTP10D175A542
ADTP10D185A615
ADTP4D21A613
ADTP47D214A870
ADTP10D147A853
ADTP47D214A695
ADTP9D201A851
ADTP8D39A670
ADTP47D204A593
ADTP28D194A656
ADTP10D54A503
ADTP10D52A631
ADTP47D214A847
ADTP4D196A929
ADTP28D194A540
ADTP47D214A821
ADTP9D46A516
ADTP8D42A434
ADTP47D214A595
ADTP47D214A847
ADTP8D44A818
ADTP47D137A527
ADTP7D32A496
ADTP10D147A853
ADTP10D54A590
ADTP7D134A249 
ADTP10D175A329
ADBP18D71 Other
ADTP8D182A382
ADTP8D182A382
ADTP8D42A434
ADTP28D194A704
ADTP28D194A704
ADTP10D213A802
ADTP10D213A802
ADTP47D145A512
ADTP9D186A386
ADTP9D46A807
ADTP9D192A669
ADTP47D214A627
ADTP10D185A615
ADTP10D54A590
ADTP3D13A856
ADTP47D204A593
ADTP47D214A870
ADTP7D134A804
ADTP10D52A631
ADTP10D52A631
ADTP47D214A870
ADTP10D213A802
ADTP10D185A619
ADT P8 D189 A845
ADT P3 D15 A923
ADT P9 D193 A561
ADT P10 D52 A815
ADT P9 D201 A454
ADT P10 D185 A615
ADT P9 D191 A429
ADT P10 D175 A547
ADT P47 D204 A917
ADT P47 D214 A695
ADT P10 D206 A886
ADT P47 D137 A 
ADT P10 D52 A525
ADT P47 D144 A860
ADT P10 D206 A480
ADT P10 D185 A528
ADT P10 D52 A631
ADT P10 D52 A631
ADT P10 D52 A631
ADT P4 D180 A630
ADT P10 D175 A542
ADT P10 D185 A615
ADT P4 D21 A613
ADT P47 D214 A870
ADT P10 D147 A853
ADT P47 D214 A695
ADT P9 D201 A851
ADT P8 D39 A670
ADT P47 D204 A593
ADT P28 D194 A656
ADT P10 D54 A503
ADT P10 D52 A631
ADT P47 D214 A847
ADT P4 D196 A929
ADT P28 D194 A540
ADT P47 D214 A821
ADT P9 D46 A516
ADT P8 D42 A434
ADT P47 D214 A595
ADT P47 D214 A847
ADT P8 D44 A818
ADT P47 D137 A527
ADT P7 D32 A496
ADT P10 D147 A853
ADT P10 D54 A590
ADT P7 D134 A249 
ADT P10 D175 A329
ADT P18 D71 Other
ADT P8 D182 A382
ADT P8 D182 A382
ADT P8 D42 A434
ADT P28 D194A 704
ADT P28 D194 A704
ADT P10 D213 A802
ADT P10 D213 A802
ADT P47 D145 A512
ADT P9 D186 A386
ADT P9 D46 A807
ADT P9 D192 A669
ADT P47 D214 A627
ADT P10 D185 A615
ADT P10 D54 A590
ADT P3 D13 A856
ADT P47 D204 A593
ADT P47 D214 A870
ADT P7 D134 A804
ADT P10 D52 A631
ADT P10 D52 A631
ADT P47 D214 A870
ADT P10 D213 A802
ADT P10 D185 A619

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

This article has been dead for over six months. Start a new discussion instead.