File I/O in C-using user input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yohan610
    New Member
    • Sep 2007
    • 5

    File I/O in C-using user input

    i have to build a program where the user enters the required input and output filenames..how do i open such files?

    tht is
    input_file = fget(.......... ..)

    finput =fopen(input_fi le,"rb")

    i cannot seem to get the user specified file to open..how do i do this???

    also how to i read bit by bit from this input file and save the encrypted bits to an output file???(this is a data encryption program....)
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by yohan610
    i have to build a program where the user enters the required input and output filenames..how do i open such files?

    tht is
    input_file = fget(.......... ..)

    finput =fopen(input_fi le,"rb")

    i cannot seem to get the user specified file to open..how do i do this???

    also how to i read bit by bit from this input file and save the encrypted bits to an output file???(this is a data encryption program....)
    Are you using a FILE * pointer.?:
    [code=c]
    FILE *fp = NULL;
    fp = fopen("text.txt ", "rb");
    if (fp == NULL)
    {
    printf("Could not open file\n");
    return 1;
    }
    [/code]
    You can read one character at a time by using fgetc().

    Comment

    • yohan610
      New Member
      • Sep 2007
      • 5

      #3
      Originally posted by ilikepython
      Are you using a FILE * pointer.?:
      [code=c]
      FILE *fp = NULL;
      fp = fopen("text.txt ", "rb");
      if (fp == NULL)
      {
      printf("Could not open file\n");
      return 1;
      }
      [/code]
      You can read one character at a time by using fgetc().
      here is my complete code
      Code:
      #include<stdio.h>
      #include<ctype.h>
      #include<string.h>
      #include<stdlib.h>
      
      FILE *finput, *foutput;
      
      int main()
      {
      	char input_file[255],output_file[255],keyword[255];
      	int seed_val=0,help=0;
      	int i = 0;
      
      	printf("\nDo u require help?(Enter '1' or '0')");
      	fgets(help,1,stdin);
      	if (help == 1);
      	{
      		printf("\Help");
      	}
      		
      		
      	printf("Enter the input file:");
      	fgets(input_file,sizeof(input_file),stdin);
      	finput = fopen(input_file,"rb");
      	for(; i < sizeof(input_file); i++)
      		printf("%c", input_file[i]);
      	
      	if (finput == NULL)
      	{
      		printf("\nError opening input file,please enter another file:");
      		while (finput == NULL)
      		{
      			printf("\nError opening input file,please enter another file:");
      			printf("\nEnter the input file:");
      			fgets(input_file,30,stdin);
      			finput = fopen(input_file,"r");
      		}
      	}
      	else printf("Input file opened succesfully\n");
      
      	printf("\nEnter the output file:");
      	fgets(output_file,30,stdin);
      	foutput = fopen(output_file,"w");
      
      	if (foutput == NULL)
      	{
      		printf("\nError opening output file,please enter another file:");
      		while (foutput == NULL)
      		{
      			printf("\nEnter the output file:");
      			fgets(output_file,30,stdin);
      			foutput = fopen(output_file,"r");
      		}
      	}
      
      	printf("\nEnter the keyword:");
      	fgets(keyword,30,stdin);
      
      	printf("\nEnter seed generator value:");
      	scanf_s("%d",&seed_val);
      	
      	printf("%s",input_file);
      
      
      }
      it just doesnt seem to work..i hope someone can help me..it always returns the value tht input file is NULL

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by yohan610
        here is my complete code
        Code:
        #include<stdio.h>
        #include<ctype.h>
        #include<string.h>
        #include<stdlib.h>
        
        FILE *finput, *foutput;
        
        int main()
        {
        	char input_file[255],output_file[255],keyword[255];
        	int seed_val=0,help=0;
        	int i = 0;
        
        	printf("\nDo u require help?(Enter '1' or '0')");
        	fgets(help,1,stdin);
        	if (help == 1);
        	{
        		printf("\Help");
        	}
        		
        		
        	printf("Enter the input file:");
        	fgets(input_file,sizeof(input_file),stdin);
        	finput = fopen(input_file,"rb");
        	for(; i < sizeof(input_file); i++)
        		printf("%c", input_file[i]);
        	
        	if (finput == NULL)
        	{
        		printf("\nError opening input file,please enter another file:");
        		while (finput == NULL)
        		{
        			printf("\nError opening input file,please enter another file:");
        			printf("\nEnter the input file:");
        			fgets(input_file,30,stdin);
        			finput = fopen(input_file,"r");
        		}
        	}
        	else printf("Input file opened succesfully\n");
        
        	printf("\nEnter the output file:");
        	fgets(output_file,30,stdin);
        	foutput = fopen(output_file,"w");
        
        	if (foutput == NULL)
        	{
        		printf("\nError opening output file,please enter another file:");
        		while (foutput == NULL)
        		{
        			printf("\nEnter the output file:");
        			fgets(output_file,30,stdin);
        			foutput = fopen(output_file,"r");
        		}
        	}
        
        	printf("\nEnter the keyword:");
        	fgets(keyword,30,stdin);
        
        	printf("\nEnter seed generator value:");
        	scanf_s("%d",&seed_val);
        	
        	printf("%s",input_file);
        
        
        }
        it just doesnt seem to work..i hope someone can help me..it always returns the value tht input file is NULL
        You shouldn't really use sizeof and you should remove the newline left by fgets():
        [code=c]
        printf("Enter the input file:");
        fgets(input_fil e, 255, stdin); /* 255; maximum of input_file */
        input_file[strlen(input_fi le) - 1] = '\0'; /* remove newline; could be done better */
        for(; i < strlen(input_fi le); i++)
        printf("%c", input_file[i]);
        finput = fopen(input_fil e,"rb");
        [/code]
        That worked for me.

        Comment

        • yohan610
          New Member
          • Sep 2007
          • 5

          #5
          Code:
          #include<stdio.h>
          #include<ctype.h>
          #include<string.h>
          #include<stdlib.h>
          
          FILE *finput, *foutput;
          
          int main()
          {
          	char input_file[255],output_file[255],keyword[255];
          	int seed_val=0,help=0;
          	int i = 0;
          
          	printf("Enter the input file:");
          	fgets(input_file,255,stdin);
          	input_file[strlen(input_file-1)] = '\0';
          
          	for (; i < strlen(input_file); i++)
          		printf("%c", input_file[i]);
          
          	finput = fopen(input_file,"rb");
          		
          	if (finput == NULL)
          	{
          		printf("\nError opening input file,please enter another file:");
          		while (finput == NULL)
          		{
          			printf("\nError opening input file,please enter another file:");
          			printf("\nEnter the input file:");
          			fgets(input_file,30,stdin);
          			fopen_s(&finput,input_file,"r");
          		}
          	}
          	else printf("Input file opened succesfully\n");
          
          	printf("\nEnter the output file:");
          	fgets(output_file,30,stdin);
          	foutput = fopen(output_file,"w");
          
          	if (foutput == NULL)
          	{
          		printf("\nError opening output file,please enter another file:");
          		while (foutput == NULL)
          		{
          			printf("\nEnter the output file:");
          			fgets(output_file,30,stdin);
          			foutput = fopen(output_file,"r");
          		}
          	}
          
          	printf("\nEnter the keyword:");
          	fgets(keyword,30,stdin);
          
          	printf("\nEnter seed generator value:");
          	scanf_s("%d",&seed_val);
          	
          	
          
          
          }
          this is my modified code, but it still says the there is an error in opening the file

          Comment

          • yohan610
            New Member
            • Sep 2007
            • 5

            #6
            sorry my bad...made a typo..now it works!!!

            Comment

            Working...