Thread: File I/o

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    File I/o

    Hi,
    the code below compiles and runs. The program reports an error incase a user provides a path name of a .doc file. the program only accepts a notepad file(.txt) in drive C. if the .txt file is in another folder, eg., c:\my documents\dominic.txt, the program reports "Invalid filename c:\my documents\dominic.txt". My aim is to have this code accept all files (irrespective of format) in my PC's disk. Kindly teach me how to achieve this.

    Code:
    #include <iostream>
    #include <string.h>
    #include <time.h>
    #include <conio.h>
    #include <fstream>
    
    #define DATA	 0x378  //Data Register base address
    #define STATUS   DATA+1 // Status Register address
    #define CONTROL  DATA+2  //Control Register address
    
    using namespace std;
    //prototyping
    void Interface (ifstream &fin, char (*Secure)(char c));
    char EncDec (char c);
    //main (): manage file stream extraction
    int main ()
    {
    	const int MaxFileNameSize=256;
    	// prompt and extract the name of the file
    	cout<<"Enter the name of the file:"<<flush;
    	char FileName[MaxFileNameSize]; //string FileName;
    	//validate the file name provided
    	cin>>FileName;
    	//ifstream  fin(Filename.c_str());
    	//converts the string type to a char array type for the function
    	ifstream fin(FileName);
    	if (fin)
    	{
    		//we have a valid file, so determine the action
    	cout<<"The file contents to be made secure through"
    		<<"bit level transformation (Y,N):"<<flush;
    
    		char reply;
    		cin>>reply;
    
    	//process the file according to the user request
    
    		switch (reply)
    
    		{
    
    		case 'Y':
    
    			Interface(fin, EncDec);
    
    			break;
    
    		case 'N':
    
    			cout<<"File is left in plain version";
    
    			break;
    
    		default:
    			{
                                     			
    			cerr<<"Bad request"<<endl;
    
    				return 1;
    			}
    			   
    		
    		}
    
    	}
    
    		//Process invalid filename.
    
    	else
    
    		{
    
    		cerr<<"Invalid file name:"<<FileName<<endl;
    
    			return 1;
    
    		}
    
    return 0;
    	}
    
    	//Interface (): Manage the interface to the stream cipher 
    	//using the EncDec () function.
    	void Interface (ifstream &fin, char (*secure)(char c))
    	{
    
    	//Extract, modify and save one-character-at-a-time 
    
    		//from the file.
    
    	char CurrentChar;  //currenrly extracted character
    
    		while (fin.get(CurrentChar))
    
    	{	//modify and save the current character using
    			//Secure()
    
    		CurrentChar=(*secure)(CurrentChar);
    
    		ofstream file_op("C:\\jacob.txt", ios::app);
    
    			file_op<<CurrentChar;
    
    		}
    
                    cout<<"EOF"<<endl;
    
    		return;
    
    	}
    
    	//EncDec(): manage writing to and reading from the 
    
    	//standard parallel port.
    
    	char EncDec (char c)
    	
    
    	{
    		char s, n;
    		s=c;
    
    outportb(CONTROL,inportb(CONTROL)&0xF0|0x04); /*initialise the
    														CONTROL port for data input*/
    
    		outportb(DATA,s);   
    /*write c to the DATA register*/
    
    		clock_t start_time;
    
    		start_time=clock();
    
    while ((clock()-start_time)<0.25*CLK_TCK);//propagation delay time
    
    n=(inportb(STATUS)&0xF0);  /* read MSnibble*/
    
    n=n|(inportb(CONTROL)&0x0F);/*Read LSnibble*/
    
    n=n^0x84; /*toggle bit 2 and 7 */
    				 					return n;
    	
    
    	}

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Word documents are different from text files. They've got markup and such in them. If you open up a *.doc in Notepad, you'll see what I'm talking about.

    *.doc is a proprietary format, so it's not something you can easily (or perhaps even legally) work with on your own; otherwise, I might say you could open up the file in binary format and try to figure it out.

    My guess is there's a way to work with *.doc files using something MS-ish like .NET.

    As for the directory issue, what do you think a program should do, search your entire hard drive? What if there are two files with the same names in different directories?

    If you want to know how traverse directories, check out the FAQ (link in sig).

    edit: You should include <string>, not <string.h>. The former contains the string class; the latter contains functions for working with C-style strings. And if you want to use the functions like strcmp() and strlen(), include <cstring>, not <string.h>.
    Last edited by joshdick; 04-27-2005 at 08:25 AM.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM