Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text files causing Headaches

Status
Not open for further replies.

GJnewbie

Technical User
Feb 26, 2005
1
GB
Hi,

This is probably as simple as it gets!
I have a text file on a server called 1.txt and this is copied to C:\scandata.txt. The contents of both files look like this.

PV-1213_-
GU-5693_-
RB 6594_-

What i am trying to do is get VB to insert a character before each line and put in some "" marks. The scandata.txt file should look like below, if all has worked.

"1","PV-1213_-"
"1","GU-5693_-"
"1","RB 6594_-"

I have tried to study Input, Print and Write commands but have hit dead ends.

Thanks for any help.

GJ
 
I don't know about what to do with the location of the files, but here is a solution for editing the contents of the files assuming both files are the folder of your project (I'm just guessing that you would use full pathnames instead of just the filenames to correct this problem but I'm not completely sure).
Notice that \" is the escape sequence for the double quotation mark.
Code:
ifstream inputFile("1.txt");

//make sure the file exists
if (!inputFile.is_open()) {
  cerr << "Error: Could not open input file" << endl;
  exit(1);
}

ofstream outputFile("scandata.txt"

//make sure the file can be created or opened for output
if (!outputFile.is_open()) {
  cerr << "Error: Could not open output file" << endl;
  exit(1);
}

string currentLine = "";
while (inputFile){
  getline(inputFile, currentLine);
  outputFile << "\"1\",\"" << currentLine << "\"" << endl;
}

That should do it. Since I haven't bothered to actually test the code, there could very well be some typing errors. Also, I don't remember the specifics of the getline command, so it might take the new line character (/n) from the end of the line and put it into the string currentLine. If this is the case, your output for each line would look like:
Code:
"1","<entry>
"
If that is the case, then there are other ways to fix it but that can be dealt with later.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top