All pastes #1046931 Raw Edit

readtxtfile

public text v1 · immutable
#1046931 ·published 2008-06-13 17:15 UTC
rendered paste body
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
    FILE *file;
    char txt_file[256]; //location
    char text[25];  //text to read from file
    char* ext = ".txt";
    
/***************askforfile()***************/    
int askforfile() ///File location procedure.
{   
    printf("\nFile?: ");
    scanf("%s",txt_file);
    if(!strstr(txt_file, ext))
    { 
     printf("Please enter a .txt file.\n");
     askforfile(); 
    }
/*    struct stat file_status;
    if(stat(txt_file,&file_size) != 0) {perror("couldn't stat"); }
    printf("\nSize of file is: %d\n",file_status.st_size); */ /*trying to learn how to use stat()*/
}
/***************askforfile()***************/   
for(;;) //main loop.  main job.
{
        askforfile();
        file = fopen(txt_file, "r");
        malloc(sizeof(file + 1));
        if ( file == NULL) 
        { 
             printf("Error in locating file.\n"); 
             askforfile(); 
        }   
        while(fgets(text, 100, file) != NULL )
        { 
        printf("\nContents: %s",text);   
        }
   }
   system("pause"); 
   fclose(file);
   return 0; 
}