-1

I am trying to read data from text file using different function like fgetc(), fgets() and fscanf(). During execution program terminates after reading from fgetc().

#include <stdio.h>

void writeFile(FILE *, char *);
void readFile(FILE *,char *);

void main(void){
    FILE *file;
    char *path="temp/test.txt";
    printf("%s\n",path);
    writeFile(file,path);
    readFile(file,path);
    return;
}

void readFile(FILE *file, char *path){
    file = fopen(path , "r");
    if(file)
        printf("\n file opened");
    char *buff;

    char getc  = fgetc(file);
    printf("\n 1 char :: %c ",getc);

    getc  = fgetc(file);
    printf("\n 2 char :: %c ",getc);
    fgetc(file); 

    fgets(buff,25,file);
    printf("\n 3 gets :: %s ",buff);

    fgets(buff,255,file);
    printf("\n 4 gets :: %s ",buff);

    int fscan = fscanf(file,"%s", buff);
    printf("\n 5 fscan :: %s ",buff);

    int eof= fclose(file);
}

void writeFile(FILE *file, char *path){
    file = fopen(path , "w+");
    if(file)
        printf("\n file opened");
    char *fileStr= "this is not working";
    int putc = fputc('@',file);
    fputc('!',file);
    int puts = fputs("\nThis is test file.",file);
    int putf1 = fprintf(file, "\n Kinldy help to solve this");
    int putf2 = fprintf(file, "\n%s", fileStr);
    int eof= fclose(file);
}

Note: If I comment writeFile(file,path); line in program, it executes properly.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 4
    `char *buff;` --> `char buff[255];` – BLUEPIXY Sep 09 '16 at 02:01
  • If you declare `buff` with `255` chars, you should also set `fscanf(file,"%254s", buff);` to insure you don't overflow (if you happened to have an unbroken sequence of characters w/o whitespace that would exceed the size of `buff` - unlikely, but to prevent undefined behavior, you should) – David C. Rankin Sep 09 '16 at 05:03

1 Answers1

-1

I made some small changes to your program so that it reads the file and doesn't get warnings. Please try if it works for you. It doesn't terminate now and hopefully this will help you.

#include <stdio.h>

void writeFile(FILE *, char *);

void readFile(FILE *, char *);

void main(void) {
    FILE *file = NULL;
    char *path = "temp/test.txt";
    printf("%s\n", path);
    writeFile(file, path);
    readFile(file, path);
    return;
}

void readFile(FILE *file, char *path) {
    file = fopen(path, "r");
    if (file)
        printf("\n file opened");
    char buff[255];

    int getc = fgetc(file);
    printf("\n 1 char :: %c ", getc);

    getc = fgetc(file);
    printf("\n 2 char :: %c ", getc);
    fgetc(file);

    fgets(buff, 25, file);
    printf("\n 3 gets :: %s ", buff);

    fgets(buff, 255, file);
    printf("\n 4 gets :: %s ", buff);

    int fscan = fscanf(file,"%254s", buff);
    printf("\n 5 fscan :: %s ", buff);

    int eof = fclose(file);
}

void writeFile(FILE *file, char *path) {
    file = fopen(path, "w+");
    if (file)
        printf("\n file opened");
    char *fileStr = "this is not working";
    int putc = fputc('@', file);
    fputc('!', file);
    int puts = fputs("\nThis is test file.", file);
    int putf1 = fprintf(file, "\n Kinldy help to solve this");
    int putf2 = fprintf(file, "\n%s", fileStr);
    int eof = fclose(file);
}
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424