Im trying to read lines in from a txt file to write into another txt file but at the moment its only reading the first line. I need it to read 1 line at a time and store it to add to another txt file before reading the next line. Id like it to read the name then the company name and store both in different varibles.
The text file we be:
Alan Jones 1ST CDR
Lucy adams 1ST CDR

double a; // When I is = to A program stops Amount of names is stored as A
    double i; 
    i=0;
    char names [50];
    char company [50];
    char count [2];
    string q;
    
  

    
    cout <<"Enter amount of names\n";
    cin.getline (count,2);
    a = atoi (count); //Converts a char to int needed when cin.getline is used
    
    
    
    
    do
    {
               i=i+1;
               fstream name;
               name.open ("names.txt", ios::in); //Opens The text document
               
               
             
               getline (name,q);
               
               
               
               fstream xml;
               xml.open("xml.txt",ios::out);
               xml <<q; //Enters The name into the Text Document
               name.close();
               xml.close();
              
               
               
    }while (i != a);

Recommended Answers

All 8 Replies

You're opening and closing the files with each iteration of the loop. When you close the file and open it again, you start reading back at the beginning.

Ive sorted that problem but how can i read the persons name and write it to a txt file and then read the company name and write it? Also i need to output a " to a text file how do i do this?

you can use 2 ' to do this instead of ", also you'd need to parse for a space or some for of identifier to tell weather is the company name
like(for which you might wanna use string stream (sstream.h) to acomplish:
if(buffer[posinstream] == " " || buffer[posinstream] == "_")
break;

Only problem that a company name may have spaces in. Also im having problems using

if(buffer[posinstream] == " " || buffer[posinstream] == "_")
break;

What do i use where posinstream is as it doesnt seem to compile?

Ive sorted that problem but how can i read the persons name and write it to a txt file and then read the company name and write it? Also i need to output a " to a text file how do i do this?

Maybe by having the company name in the input file? And if all you are doing is copying the file, why do you need to output a "?

Maybe you need to explain what you are trying to do and not give us small pieces of the problem that don't make sense all by themselves.

Have you read Read Me: Read This Before Posting yet?

This

if(buffer[posinstream] == " " || buffer[posinstream] == "_")

should be

if(buffer[posinstream] == ' ' || buffer[posinstream] == '_')

Only problem that a company name may have spaces in. Also im having problems using

if(buffer[posinstream] == " " || buffer[posinstream] == "_")
break;

What do i use where posinstream is as it doesnt seem to compile?

sorry, some ambiguity on my part, you need to read form the file char by char (or from a buffer) in a loop, where posinstream is the loop count. also if the names might have spaces you can use tab ("\t") as a delimiter.

This

if(buffer[posinstream] == " " || buffer[posinstream] == "_")

should be

if(buffer[posinstream] == ' ' || buffer[posinstream] == '_')

Ah thanks :) was think of stringstream when i wrote that....

Maybe by having the company name in the input file? And if all you are doing is copying the file, why do you need to output a "?

Maybe you need to explain what you are trying to do and not give us small pieces of the problem that don't make sense all by themselves.

Have you read Read Me: Read This Before Posting yet?

Ok i work for a printing company and we print CD's for seminar's with a different name on each one so we need a program that can read a list of name's + companys from a text file to put them into a file with xml code so our software can read it and put each name on to a different CD.

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.