c++ Linux2Windows text file conversion

kalio80_at_hotmail.com
Date: 12/08/04


Date: 7 Dec 2004 22:03:26 -0800

Hi everyone I posted an enquiry earlier about using c++ code to convert
text files between linux & widows. I ended up with this code:

#include <iostream>
#include <fstream>

using namespace std;

int uw(string src,string dest);
int wu(string src,string dest);
int helpfile();

// User options and fucntion call out.
int main(int argc, char * argv[]){

if(argc==2){

string function(argv[1]);
if(function == "/?"){
int ans = helpfile();
if (ans == 1){
cout << " Error opening source\n";
}
}else{
cout << "Usage: prog (/uw | /wu | /?) source dest\n"<<endl;
return 1;
}

}else if(argc == 4){

string function(argv[1]);
string src(argv[2]);
string dest(argv[3]);

if(function == "/uw"){
uw(src,dest); // calls for converting a Unix text file to Windows
Format.
int ans = uw(src,dest);
if(ans == 0){
cout << "Done\n";
}else if(ans == 1){
cout << " Error opening source\n";
}else if(ans == 2){
cout << "Error: Source and Destination must be different\n";
}else if(ans == 3){
cout << "Error creating destination file \n";
}
}else if(function == "/wu"){
wu(src,dest); // Calls for converting a Windows test file to Unix
format.
int ans = uw(src,dest);
if(ans == 0){
cout << "Done\n";
}else if(ans == 1){
cout << " Error opening source\n";
}else if(ans == 2){
cout << "Error: Source and Destination must be different\n";
}else if(ans == 3){
cout << "Error creating destination file \n";
}
}else {
cout << "Usage: prog (/uw | /wu | /?) source dest\n";
}
}else{
cout << "Usage: prog (/uw | /wu | /?) source dest\n";

}
return 1;
}

// Converting Unix text files to Windows format
int uw(string src, string dest){

if(src == dest){
return 2;
}

ifstream in(src.c_str(),ios::binary | ios::in);
if(!in){
return 1;
}

ofstream out(dest.c_str(),ios::binary | ios::out);
if(!out){
return 3;
}

//looks for LineFeeds and puts Carriage returns in front of it.
char c;
while(in.get(c))
{
if(c == 10){
out.put(13);
out.put(10);
return 0;
}else{
out.put(c);
}
}

in.close();
out.close();
return 0;
}

// Converts Windows text files to Unix format.
int wu(string src, string dest){

if(src == dest){
return 2;
}
ifstream in(src.c_str(),ios::binary | ios::in);
if(!in){
return 1;
}

ofstream out(dest.c_str(),ios::binary | ios::out);
if(!out){
return 3;
}

char c;
while(in.get(c))
{
if(c != 13)
{
out.put(c);
return 0;
}
}
in.close();
out.close();
return 0;
}

//Displays the Help file.
int helpfile(){

ifstream inf("help.txt",ios::in);
if(!inf){
return 1;
}
string theLine = "";
while(getline(inf,theLine)){
cout << theLine << endl;
}
inf.close();
return 0;

}
so typing swapf /uw unix.txt windows.txt will convert a text file from
unix to windows.

when I try to do convert from windows to unix, the new file will only
display the first line choppig the rest of the file. I can't see why
it's doing this, can you please give me some advice on that.
kind regards

kalio



Relevant Pages

  • Help With TreeMap Warning (Noob?)
    ... public static int naive_editDistance(String Source, String Dest){ ... int OSL){ ...
    (comp.lang.java.programmer)
  • Re: Printing unsigned long long ints
    ... > I'm trying to convert a string to an unsigned long long int. ... strtoul returns a 'long', ... If cout really was the part that doesn't work, ...
    (comp.lang.cpp)
  • Re: split string into chunks
    ... int nbr; char buff; ... Split a string into several lines with each line being ... @param dest Destination buffer ...
    (comp.lang.c)
  • Re: (silly?) speed comparisons
    ... I guess the point is to make a vector of referene to string if you don't want to copy string objects all around but just a word for an address each time. ... int idx; ...
    (comp.lang.python)
  • Re: Finding longest and most repeated substring
    ... using namespace std; ... int main ... string text; ... cout << endl; ...
    (comp.programming)