c++ conversion files

From: kalio80 (kalio80_at_hotmail.com)
Date: 12/04/04


Date: 3 Dec 2004 20:56:23 -0800

Hi everyone I am trying to create a file that converts text files from
unix to windows and windows to unix
I understand the general concept of it as
unix uses line feed LF
Windows uses CRLF carrige return and feed line
my program will prompt the user to enter a file to open and then
prompts for a destination file to save the new formatted file.
I am having few problems that i have been trying to solve for few
hours
here is the code I created
#include <iostream>
#include <fstream>
using namespace std;
 
const string unix = "/uw";
const string windows = "/wu";
const string help = "/?";
int uw();
int wu();
int helpfile();
 
int main()
{
 
  
 string option;
 cout << "\n";
 cout << "************************ Menu *********************\n";
 cout << "\n";
 cout << " Please choose from the following list\n";
 cout << "\n";
 cout << " * type /uw to convert a file from Unix to Windows\n";
 cout << " * type /wu to convert a file from Windows to Unix\n";
 cout << " * type Help to Display the help file\n";
 getline(cin,option);
 
 
  while((option != unix) && (option!= windows) && (option != help) ){
  cout << " that's not a valid option! Try again\n";
  getline(cin,option);
  }
 
 if (option == unix){
  uw();
 }else if(option == windows){
  wu();
 }else if(option == help){
  helpfile();
 }
 
 return 0;
}
 

 
int uw(){
 char FileName[20];
 char Destination[20];
 cout << "please enter the name of the source file: \n";
 cin >> FileName;
  
ifstream in(FileName,ios::binary | ios:: in);
 if(!in){
  cout << "Error opening source\n";
 return 0;
 }
 
 cout << "please enter the name of the destination file: \n";
 cin >> Destination;
while(FileName == Destination){
  cout << "Source and Destination file names must be different please
try another:\n";
 cin >> Destination;
 }
 ofstream out(Destination,ios::binary | ios::out);
  if(!out){
  cout << "Error creating destination file \n";
 return 0;
 }
 
 
 char c;
 
 while(in.get(c)){
  out.put(c);
  if(c == 13){
   cout << "Carriage return, not a Unix text file please reconsider
option\n";
  }
  if(c == 10){
   cout << "Line feed\n";
  }
 }
  
 in.close();
 out.close();
 
 
 
 return 0;
}
 
 
 
 
int wu(){
 char FileName[20];
 char Destination[20];
 cout << "please enter the name of the source file (i.e.) file1.txt
\n";
 cin >> FileName;
ifstream in(FileName ,ios::binary | ios:: in);
 
 if(!in){
  cout << "Error opening source\n";
  return 1;
 }
 cout << "please enter the name of the destination file: \n";
 cin >> Destination;
 
 while(FileName == Destination){
  cout << "Source and Destination file names must be different please
try another:\n";
 cin >> Destination;
 }
 ofstream out(Destination,ios::binary | ios::out);
 if(!out){
  cout << "Error creating destination file \n";
  return 0;
 }
 
 char c;
 
 while(in.get(c)){
  out.put(c);
  if(c == 13){
   cout << "Carriage return\n";
  }
  if(c == 10){
   cout << "Line feed\n";
  }
 }
  
 in.close();
 out.close();
 
 
 
 return 0;
 
 
}
 
 
 
int helpfile(){
 
 //system("pause");
 cout << "\n\nHere is c:\\help.txt \n\n";
 ifstream inf("c:\\help.txt",ios::in);
 if(!inf){
  cout << "Error reading file\n";
  return 1;
 }
 string theLine = "";
 while(getline(inf,theLine)){
  cout << theLine << endl;
 }
 inf.close();
 
 return 0;
 
}



Relevant Pages