Thread: file I/O

  1. #1
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    file I/O

    I am trying to write the content of a document that the user specify into a new file which has teh same name but with teh extension .new

    How can i do that? I have marked <======= to show where the problem is.


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    FILE *ofp;
    FILE *ifp;
    
    int main(int argc, char*argv[])
    {
    	int ch;
    	char *document;
    
    	if (argc==2)
            {
    		document=malloc(strlen(argv[1]));
    	    strcpy (document, argv[1]);
    		}
    	else {perror("date2");exit(1);}
    	
    	ifp=fopen(document,"r");
    	ofp=fopen(document.new,"w");     <=========
    	
    	if(ifp!='\0')
    	{
    		while ((ch=fgetc(ifp))!=EOF)
    		{		       
    		fputc(ch,ofp);
    		}		
    	}
    	else {printf("\nError Opening File.\n");}
    
    	free(document);
    	fclose(ifp);
    	fclose(ofp);
    
    	return 0;
    }

  2. #2
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Thanks Salem....

    How do I put the \0 at the end of the string....(I can never get it!!!!) and I thought c does it automatically

    Yes you are right I want to change any file that the user uses into .new and not file .txt.new....

    Iam going to work on it now.....

  3. #3
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    OK I have reached a dead end.....

    How can I change what the file extention that teh use typed at the comand prompt.

    ex: if typed output.txt, I want my programto create output.new
    currently my program does this: output.txt.new which is not what I want.


    I am also trying to insert in teh output file letters:
    The input file has numbers like that:
    12
    13
    14
    15

    I want to create an output file with extension .new that assign a letter after each 2 number lines
    like that:
    12 a
    13 b
    14
    15
    16 a
    17 b
    ...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    FILE *ofp;
    FILE *ifp;
    
    int main(int argc, char*argv[])
    {
    	int ch;
    	char *document;
    
    	if (argc==2)
            {
    		document=malloc(strlen(argv[1]) + sizeof(".new")+1);
    	    strcpy (document, argv[1]);
    		}
    	else {perror("date2");exit(1);}
    	
    	ifp=fopen(document,"r");
    	strcat(document, ".new");
    	ofp=fopen(document,"w");
    	
    	if(ifp!='\0')
    	{
    		while ((ch=fgetc(ifp))!=EOF)
    		{		       
    			fputc(ch,ofp);
    			fseek(ofp,7,SEEK_SET);
    		                    fprintf(ofp,"here");
    		}		
    	}
    	else {fprintf(stderr,"date2:Error Opening File.\n"); exit(1);}
    
    	free(document);
    	fclose(ifp);
    	fclose(ofp);
    
    	return 0;
    }

  4. #4
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    I am trying to insert into the output file letters:

    The input file has numbers like that:
    12
    13
    14
    15

    I want to create an output file with extension .new (which I did!)that assign a letter after each 2 number lines
    like that:
    12 a
    13 b
    14
    15
    16 a
    17 b
    ...

    Need help....Thanks

    Here is my latest code


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    FILE *ofp;
    FILE *ifp;
    
    int main(int argc, char*argv[])
    {
    	int k;
    	char fileline[50];
    	char *document;
    	char *p;
    
    	if (argc==2)
            {
    		document=malloc(strlen(argv[1]) + sizeof(".new")+1);
    	    strcpy (document, argv[1]);
    		}
    	else {perror("date2");exit(1);}
    	
    	ifp=fopen(document,"r");
    	
    	p = strrchr(document,'.');
    	if (p !=NULL ) *p = '\0';
    	strcat(document, ".new");
    
    	ofp=fopen(document,"w");		
    	
                fgets(fileline,sizeof(fileline),ifp);
               	fputs(fileline,ofp);
    			fseek(ofp,4,SEEK_SET);
    			fprintf(ofp,"a");
    
    			rewind(ifp);
    			fseek(ifp,2,SEEK_SET);
    			fgets(fileline,sizeof(fileline),ifp);
              	fputs(fileline,ofp);
    			//fseek(ofp,4,SEEK_SET);
    			//fprintf(ofp,"b");
    
    		
    	free(document);
    	fclose(ifp);
    	fclose(ofp);
    
    	return 0;
    }

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