Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

how to read from a file in c

FILE *in_file  = fopen("name_of_file", "r"); // read only 
          FILE *out_file = fopen("name_of_file", "w"); // write only 
           
          // test for files not existing. 
          if (in_file == NULL || out_file == NULL) 
            {   
              printf("Error! Could not open file
"); 
              exit(-1); // must include stdlib.h 
            } 
           
          // write to file vs write to screen 
          fprintf(file, "this is a test %d
", integer); // write to file 
 
          fprintf(stdout, "this is a test %d
", integer); // write to screen  
          printf(         "this is a test %d
", integer); // write to screen  
 
          // read from file/keyboard. remember the ampersands!  
          fscanf(file, "%d %d", &int_var_1, &int_var_2);  
 
          fscanf(stdin, "%d %d", &int_var_1, &int_var_2);  
          scanf(        "%d %d", &int_var_1, &int_var_2);
Source by www.cs.utah.edu #
 
PREVIOUS NEXT
Tagged: #read #file
ADD COMMENT
Topic
Name
2+7 =