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: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: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
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: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: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