c++ Linux2Windows text file conversion

K

kalio80

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::eek:ut);
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::eek:ut);
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
 
K

kalio80

Nice advice... but you see, I am doing this for fun so please don't
spoil my fun
tuh
 
C

Craig Nicol

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

[snip]

// 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::eek:ut);
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;
^^^ is this your problem? Did you mean to return after finding a new line?
(BTW, there's a similar problem in the wu function)
}else{
out.put(c);
}
}

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


[snip]

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
You could also look at reading in line-by-line from a stream, that might
make things easier...

Craig Nicol.
 
K

kalio80

I actually found what was wrong with it
it was just a stupid typing error. rather than typing a u typed a w
that was it
one and only one reason why I some times hate programming
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top