By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,106 Members | 2,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,106 IT Pros & Developers. It's quick & easy.

File I/O in C-using user input

P: 5
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_file,"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....)
Sep 9 '07 #1
Share this Question
Share on Google+
5 Replies


ilikepython
Expert 100+
P: 844
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_file,"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.?:
Expand|Select|Wrap|Line Numbers
  1. FILE *fp = NULL;
  2. fp = fopen("text.txt", "rb");
  3. if (fp == NULL)
  4. {
  5.     printf("Could not open file\n");
  6.     return 1;
  7. }
  8.  
You can read one character at a time by using fgetc().
Sep 10 '07 #2

P: 5
Are you using a FILE * pointer.?:
Expand|Select|Wrap|Line Numbers
  1. FILE *fp = NULL;
  2. fp = fopen("text.txt", "rb");
  3. if (fp == NULL)
  4. {
  5.     printf("Could not open file\n");
  6.     return 1;
  7. }
  8.  
You can read one character at a time by using fgetc().
here is my complete code
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5.  
  6. FILE *finput, *foutput;
  7.  
  8. int main()
  9. {
  10.     char input_file[255],output_file[255],keyword[255];
  11.     int seed_val=0,help=0;
  12.     int i = 0;
  13.  
  14.     printf("\nDo u require help?(Enter '1' or '0')");
  15.     fgets(help,1,stdin);
  16.     if (help == 1);
  17.     {
  18.         printf("\Help");
  19.     }
  20.  
  21.  
  22.     printf("Enter the input file:");
  23.     fgets(input_file,sizeof(input_file),stdin);
  24.     finput = fopen(input_file,"rb");
  25.     for(; i < sizeof(input_file); i++)
  26.         printf("%c", input_file[i]);
  27.  
  28.     if (finput == NULL)
  29.     {
  30.         printf("\nError opening input file,please enter another file:");
  31.         while (finput == NULL)
  32.         {
  33.             printf("\nError opening input file,please enter another file:");
  34.             printf("\nEnter the input file:");
  35.             fgets(input_file,30,stdin);
  36.             finput = fopen(input_file,"r");
  37.         }
  38.     }
  39.     else printf("Input file opened succesfully\n");
  40.  
  41.     printf("\nEnter the output file:");
  42.     fgets(output_file,30,stdin);
  43.     foutput = fopen(output_file,"w");
  44.  
  45.     if (foutput == NULL)
  46.     {
  47.         printf("\nError opening output file,please enter another file:");
  48.         while (foutput == NULL)
  49.         {
  50.             printf("\nEnter the output file:");
  51.             fgets(output_file,30,stdin);
  52.             foutput = fopen(output_file,"r");
  53.         }
  54.     }
  55.  
  56.     printf("\nEnter the keyword:");
  57.     fgets(keyword,30,stdin);
  58.  
  59.     printf("\nEnter seed generator value:");
  60.     scanf_s("%d",&seed_val);
  61.  
  62.     printf("%s",input_file);
  63.  
  64.  
  65. }
it just doesnt seem to work..i hope someone can help me..it always returns the value tht input file is NULL
Sep 10 '07 #3

ilikepython
Expert 100+
P: 844
here is my complete code
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5.  
  6. FILE *finput, *foutput;
  7.  
  8. int main()
  9. {
  10.     char input_file[255],output_file[255],keyword[255];
  11.     int seed_val=0,help=0;
  12.     int i = 0;
  13.  
  14.     printf("\nDo u require help?(Enter '1' or '0')");
  15.     fgets(help,1,stdin);
  16.     if (help == 1);
  17.     {
  18.         printf("\Help");
  19.     }
  20.  
  21.  
  22.     printf("Enter the input file:");
  23.     fgets(input_file,sizeof(input_file),stdin);
  24.     finput = fopen(input_file,"rb");
  25.     for(; i < sizeof(input_file); i++)
  26.         printf("%c", input_file[i]);
  27.  
  28.     if (finput == NULL)
  29.     {
  30.         printf("\nError opening input file,please enter another file:");
  31.         while (finput == NULL)
  32.         {
  33.             printf("\nError opening input file,please enter another file:");
  34.             printf("\nEnter the input file:");
  35.             fgets(input_file,30,stdin);
  36.             finput = fopen(input_file,"r");
  37.         }
  38.     }
  39.     else printf("Input file opened succesfully\n");
  40.  
  41.     printf("\nEnter the output file:");
  42.     fgets(output_file,30,stdin);
  43.     foutput = fopen(output_file,"w");
  44.  
  45.     if (foutput == NULL)
  46.     {
  47.         printf("\nError opening output file,please enter another file:");
  48.         while (foutput == NULL)
  49.         {
  50.             printf("\nEnter the output file:");
  51.             fgets(output_file,30,stdin);
  52.             foutput = fopen(output_file,"r");
  53.         }
  54.     }
  55.  
  56.     printf("\nEnter the keyword:");
  57.     fgets(keyword,30,stdin);
  58.  
  59.     printf("\nEnter seed generator value:");
  60.     scanf_s("%d",&seed_val);
  61.  
  62.     printf("%s",input_file);
  63.  
  64.  
  65. }
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():
Expand|Select|Wrap|Line Numbers
  1.     printf("Enter the input file:");
  2.     fgets(input_file, 255, stdin);               /* 255; maximum of input_file */
  3.     input_file[strlen(input_file) - 1] = '\0';  /* remove newline; could be done better */
  4.     for(; i < strlen(input_file); i++)
  5.         printf("%c", input_file[i]);
  6.     finput = fopen(input_file,"rb");
  7.  
That worked for me.
Sep 10 '07 #4

P: 5
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5.  
  6. FILE *finput, *foutput;
  7.  
  8. int main()
  9. {
  10.     char input_file[255],output_file[255],keyword[255];
  11.     int seed_val=0,help=0;
  12.     int i = 0;
  13.  
  14.     printf("Enter the input file:");
  15.     fgets(input_file,255,stdin);
  16.     input_file[strlen(input_file-1)] = '\0';
  17.  
  18.     for (; i < strlen(input_file); i++)
  19.         printf("%c", input_file[i]);
  20.  
  21.     finput = fopen(input_file,"rb");
  22.  
  23.     if (finput == NULL)
  24.     {
  25.         printf("\nError opening input file,please enter another file:");
  26.         while (finput == NULL)
  27.         {
  28.             printf("\nError opening input file,please enter another file:");
  29.             printf("\nEnter the input file:");
  30.             fgets(input_file,30,stdin);
  31.             fopen_s(&finput,input_file,"r");
  32.         }
  33.     }
  34.     else printf("Input file opened succesfully\n");
  35.  
  36.     printf("\nEnter the output file:");
  37.     fgets(output_file,30,stdin);
  38.     foutput = fopen(output_file,"w");
  39.  
  40.     if (foutput == NULL)
  41.     {
  42.         printf("\nError opening output file,please enter another file:");
  43.         while (foutput == NULL)
  44.         {
  45.             printf("\nEnter the output file:");
  46.             fgets(output_file,30,stdin);
  47.             foutput = fopen(output_file,"r");
  48.         }
  49.     }
  50.  
  51.     printf("\nEnter the keyword:");
  52.     fgets(keyword,30,stdin);
  53.  
  54.     printf("\nEnter seed generator value:");
  55.     scanf_s("%d",&seed_val);
  56.  
  57.  
  58.  
  59.  
  60. }
  61.  
this is my modified code, but it still says the there is an error in opening the file
Sep 10 '07 #5

P: 5
sorry my bad...made a typo..now it works!!!
Sep 10 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.