Click here to Skip to main content
15,922,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What's the best way to get rid of, I believe they're called "carriage returns" (at the end of the file, it automatically creates a new line), in a file? I am reading from a file, parsing and tokenizing data, but it gets messed up due to the carriage return.

Edit:: I actually just had the program read through the file and create an integer corresponding to the total number of lines and then just had a loop associated with a modified version of the integer.

--------------------------------------

Trying to put these two codes together, but unsure how:

Code one:

C++
// reading a text file
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
using namespace std;
int main () {
	string teststr, teststr1;
	string stop;
    ifstream incsv ("example.csv"); 
    ofstream outtxt ("examplet.txt");
  if (incsv.is_open())
  {
	  int x=0;
	  while (x<5)
	  {
		  getline(incsv, stop, '\n');
		  x=(x + 1);
	  }
	  
	  
	  while (!incsv.eof())
		  {
   	    getline(incsv, teststr, '\"');
		replace(teststr.begin(), teststr.end(), ',', '|');
		getline(incsv, teststr1, '\n');
		replace(teststr1.begin(), teststr1.end(), '\"', ',');
		outtxt << teststr << teststr1 << endl;
	  }
	  incsv.close();
		  return 0;
  }
}



Second Code:

C++
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <climits>
#include <algorithm>
#include "stdlib.h"
#include <sstream>
using namespace std;
int main () {
   
   ifstream intig ("example12.txt");
   ofstream outbatty ("testing.bat");
  if (intig.is_open())
    {
        while (getline(intig, line))
        {
            // copy the string to a character array
            char* pszLine = new char[line.length() + 1];
            strcpy(pszLine, line.c_str());
            char* szToken[6];
 
            // split the line into token
            szToken[0] = strtok(pszLine, "|");
            for (int i = 1; i < 6; ++i)
            {
                szToken[i] = strtok(NULL, "|");
            }
 
            // write the tokens out in a different order
            
			outbatty << str1 << szToken[2] << " " << szToken[3] << " " << szToken[0] << " " << szToken[1] << " " ;
			outbatty << szToken[4] << "," << szToken[5] << str2 << szToken[0] << "." << szToken[2] << str3 << szToken[0] << " " ;
			outbatty << str4 << szToken[1] << str5 << szToken[2] << str6 << szToken[2] << " " << szToken[3] << " " << szToken[0];
			outbatty << " " << szToken[1] << " " << szToken[4] << str7 << szToken[4] << str8 << endl;			
			// tidy up
            delete []pszLine;
        }
}
 
}
Posted
Updated 12-Aug-11 15:49pm
v8
Comments
JeremyMH 12-Aug-11 15:56pm    
Thanks, Chris. Kept eating my code when I was trying to display the code tags.

Why not throw program 1 away completely and do all the work in program 2 like this:
1. Open the CSV and BAT files as required.
2. Read the next line from the CSV file.
3. Replace commas by vertical bars
4. Replace double quotes by commas
5. Split the (bar separated) line into tokens
6. Write the re-ordered tokens to the BAT file
7. continue until end of input

When you have created (and closed) the BAT file you can send it direct to the CMD processor via the system()[^] function.
 
Share this answer
 
Comments
JeremyMH 12-Aug-11 18:11pm    
In your opinion, what's the best way to get rid of, I believe they're called "carriage returns" (at the end of the file, it automatically creates a new line), in a file?
JeremyMH 12-Aug-11 21:47pm    
I actually got the program up and fuctional doing everything that it needs to do without having to rely on any other exe's or files, so problem solved!

Thanks again for all your help, haha.
Do you want to get rid of all of them and just have a single line output?

Then I'd write plain old C code:

main()
{
    int c;

    while ((c = getchar()) != EOF) {
       if (c != '\n' && c != '\r') 
           putchar(c);
    }
}


If on the other hand, you want to just eliminate the newlines at the end of the output of the programs you described above, then just leave off the << endl:
outbatty << str1 << szToken[2] << " " << szToken[3] << " " << szToken[0] << " " << szToken[1] << " " ;

outbatty << szToken[4] << "," << szToken[5] << str2 << szToken[0] << "." << szToken[2] << str3 << szToken[0] << " " ;

outbatty << str4 << szToken[1] << str5 << szToken[2] << str6 << szToken[2] << " " << szToken[3] << " " << szToken[0];

outbatty << " " << szToken[1] << " " << szToken[4] << str7 << szToken[4] << str8 /* << endl */;


Or are you just trying to get rid of the last newline in the file?

In that case, the usual thing is to rearrange your logic so you output a newline at the start of each line instead of the end of each line. (If you don't want an extra newline at the beginning then add logic to not output a newline the first time.)
 
Share this answer
 
Comments
JeremyMH 12-Aug-11 20:19pm    
Actually, I retract that. With CSV files, which I am working with, there is an automatic empty line at the end of the file. That's what I need to erase.
JeremyMH 12-Aug-11 21:03pm    
Nvm nvm. I had my program read through the file, get a number of lines and then set a loop relative to the integer it generated. Problem solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900