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

file processing: custom name problem for i/o

revolter00
P: 2
hi all, this is a piece of code from the program that i`m currently working on..

Expand|Select|Wrap|Line Numbers
  1. .
  2. .
  3. #define SIZE 10 
  4. .
  5. .
  6. .
  7.     int opt, NumARR[SIZE], L, MaxINT, sum=0, input_int;
  8.  
  9.     char *FileNameSTR[20];
  10.     FILE *fileP; 
  11. .
  12. .
  13. .
  14.  
  15. // Option 2  'File I/O'
  16.          else if(opt==2) {
  17.  
  18.                   // Read from file          
  19.                   printf("Enter a file name to read: ");
  20.                   gets(*FileNameSTR);      
  21.  
  22.                   if((fileP=fopen("FileNameSTR","r"))==NULL)
  23.                        printf("\n> Specified file not found!\n\n");
  24.  
  25.                   else {
  26.                        printf("Integers in file:\n");
  27.                        for(L=1;L<=SIZE;L++) 
  28.                               fscanf(*FileNameSTR,"%d",&NumARR[L]);
  29.  
  30.  
  31.                        while(!feof(FileNameSTR[20])) {
  32.                               for(L=1;L<=SIZE;L++) { printf("%d\n",NumARR[L]); }                 
  33.                                                  }
  34.                        fclose(FileNameSTR);
  35.                        printf("\n> Reading process done.\n\n");
  36.                        } // end of else
  37.  
  38.  
  39.                   // Write to file     
  40.                   printf("Enter a file name to read: ");
  41.                   gets(*FileNameSTR);        
  42.  
  43.                   if((fileP=fopen("FileNameSTR","w"))==NULL)
  44.                        printf("\n> Specified file not found!\n\n");     
  45.  
  46.                   else {
  47.                        for(L=1;L<=SIZE;L++) {
  48.                              printf("Int #%d: ",L); 
  49.                              scanf("%d",&NumARR[L]);  
  50.                                            }
  51.  
  52.                        while(!feof(stdin)) {
  53.                               for(L=1;L<=SIZE;L++) { fprintf(FileNameSTR,"%d\n",NumARR[L]); }            
  54.                                            }                    
  55.                        fclose(FileNameSTR);
  56.                        printf("\n> Reading process done.\n\n");              
  57.                        } // end of else
  58.  
  59.                     } // end of if(2)
  60.  
this code should ask
1- a filename to read 10 lines of integers from that file, then prints them to screen
2- a filename to write 10 integers from keyboard to that new file

but there is a problem on getting the file name, which is (i think) about pointer related..

Dev-C gives these errors:
"[Warning] passing arg 1 of `fscanf' from incompatible pointer type "
"[Warning] passing arg 1 of `fclose' from incompatible pointer type "
"[Warning] passing arg 1 of `fprintf' from incompatible pointer type "
"[Warning] passing arg 1 of `fclose' from incompatible pointer type "


any help will be appreciated, thanks in advance..
Feb 21 '07 #1
Share this Question
Share on Google+
2 Replies


100+
P: 1,646
Hi. It may work better if you declare your file string like this:
Expand|Select|Wrap|Line Numbers
  1. char FileNameSTR[50];
Then use it like this:
Expand|Select|Wrap|Line Numbers
  1. gets(FileNameSTR);  
and this:
Expand|Select|Wrap|Line Numbers
  1. fscanf(FileNameSTR,"%d",&NumARR[L]);
Feb 21 '07 #2

revolter00
P: 2
willakawill, thank you very much for the help, especially char FileNameSTR[50] part.

for other errors, i handled with them by replacing function args as
fileP, which is defined as a pointer to the file.

and modified code is above, no syntax error, but now there is a problem about reading file: "it never stops reading"



Expand|Select|Wrap|Line Numbers
  1. .
  2. .
  3. #define SIZE 10 
  4. .
  5. .
  6. .
  7.     int opt, NumARR[SIZE], L, MaxINT, sum=0, input_int;
  8.  
  9.     char FileNameSTR[20];
  10.     FILE *fileP; 
  11. .
  12. .
  13. .
  14.          // Option 2  'File I/O'
  15.          else if(opt==2) {
  16.  
  17.                   // Read from file          
  18.                   printf("Enter a file name to read: ");
  19.                   fflush(stdin);  // skip gets() without flush
  20.                   gets(FileNameSTR);      
  21.  
  22.                   if((fileP=fopen(FileNameSTR,"r"))==NULL)
  23.                        printf("\n> Specified file not found!\n\n");
  24.  
  25.                   else {
  26.                        printf("Integers in file:\n");
  27.                        for(L=1;L<=SIZE;L++) 
  28.                               fscanf(fileP,"%d",&NumARR[L]);
  29.  
  30.  
  31.                        while(!feof(fileP)) {
  32.                               for(L=1;L<=SIZE;L++) { printf("%d\n",NumARR[L]); }                 
  33.                                                  }
  34.                        fclose(fileP);
  35.                        printf("\n> Reading process done.\n\n");
  36.                        } // end of else
  37.  
  38.  
  39.                   // Write to file     
  40.                   printf("Enter a file name to read: ");
  41.                   gets(FileNameSTR);          
  42.  
  43.                   if((fileP=fopen(FileNameSTR,"w"))==NULL)
  44.                        printf("\n> Specified file not found!\n\n");     
  45.  
  46.                   else {
  47.                        for(L=1;L<=SIZE;L++) {
  48.                              printf("Int #%d: ",L); 
  49.                              scanf("%d",&NumARR[L]);  
  50.                                            }
  51.  
  52.                        while(!feof(stdin)) {
  53.                               for(L=1;L<=SIZE;L++) { fprintf(fileP,"%d\n",NumARR[L]); }            
  54.                                            }                    
  55.                        fclose(fileP);
  56.                        printf("\n> Reading process done.\n\n");              
  57.                        } // end of else
  58.  
  59.                     } // end of if(2)
  60.  
i think i can handle with this; but any better solutions and suggestions will be appreciated, thanks again.
Feb 21 '07 #3

Post your reply

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