0

so i have a problem that fscanf won't read after I make fprintf process become a comment. So when I open the file with r+, it wouldn't read. Here is the for specific problem:

int main()
{
    FILE *fp;
    char test='T',check3rd,optreg='T',cekbingung[1000][100];
    int x;
    char namalengkap[50],gender[50],charnamafile[50],tempat[50],temp[1000];
    int umur,tb,bb;
    /*fp=fopen("filetest4.txt","w+");
    printf("nama : ");
    fgets(namalengkap,100,stdin);
    strtok(namalengkap,"\n");
    printf("gender : ");
    fgets(gender,100,stdin);
    strtok(gender,"\n");
    printf("umur : ");
    scanf("%d",&umur);
    fflush(stdin);
    printf("tb : ");
    scanf("%d",&tb);
    fflush(stdin);
    printf("bb : ");
    scanf("%d",&bb);
    fflush(stdin); 
    fprintf(fp,"====================DATA PRIBADI USER=================\n");
    fprintf(fp,"= Nama Lengkap      : %-30s =\n",namalengkap);
    fprintf(fp,"= Jenis Kelamin     : %-30s =\n",gender);
    fprintf(fp,"= Umur              : %-30d =\n",umur);
    fprintf(fp,"= Tinggi Badan (CM) : %-30d =\n",tb);
    fprintf(fp,"= Berat  Badan (KG) : %-30d =\n",bb);
    fprintf(fp,"======================================================\n");

    rewind(fp);*/
    fp=fopen("filetest4.txt","r+");
    fscanf(fp,"====================DATA PRIBADI USER=================\n");
    fscanf(fp,"= Nama Lengkap      : %-30[^\n] =\n",namalengkap);
    fscanf(fp,"= Jenis Kelamin     : %-30[^\n] =\n",gender);
    fscanf(fp,"= Umur              : %-30d =\n",&umur);
    fscanf(fp,"= Tinggi Badan (CM) : %-30d =\n",&tb);
    fscanf(fp,"= Berat  Badan (KG) : %-30d =\n",&bb);
    fscanf(fp,"======================================================\n");

    printf("=DATA PRIBADI USER=\n");
    printf("=Nama Lengkap      : %s =\n",namalengkap);
    printf("=Jenis Kelamin     : %s =\n",gender);
    printf("=Umur              : %d =\n",umur);
    printf("=Tinggi Badan (CM) : %d =\n",tb);
    printf("=Berat  Badan (KG) : %d =\n",bb);
    return 0;
}

First run, before make fprintf and other scan input become a comment, it success scan the file:

nama : file testing 4
gender : male
umur : 20
tb : 100
bb : 24
=DATA PRIBADI USER=
=Nama Lengkap      : file testing 4 = =
=Jenis Kelamin     : male =
=Umur              : 20 =
=Tinggi Badan (CM) : 100 =
=Berat  Badan (KG) : 24 =
===========================

Process returned 0 (0x0)   execution time : 14.516 s
Press any key to continue.

This is filetest4.txt content:

1. ====================DATA PRIBADI USER=================
2. = Nama Lengkap      : file testing 4                 =
3. = Jenis Kelamin     : male                           =
4. = Umur              : 20                             =
5. = Tinggi Badan (CM) : 100                            =
6. = Berat  Badan (KG) : 24                             =
7. ======================================================

But when I make fprintf and other scan input into a comment like the code that I show, It becomes error like this:

=DATA PRIBADI USER=
=Nama Lengkap      : file testing 4 = =
=Jenis Kelamin     :  =
=Umur              : 0 =
=Tinggi Badan (CM) : 0 =
=Berat  Badan (KG) : 0 =
===========================

Process returned 0 (0x0)   execution time : 0.046 s
Press any key to continue.

I already tried using fgets, i don't know that i did it right or wrong:

fgets(temp,sizeof temp,fp);
sscanf(temp,"= Nama Lengkap      : %[^\n] =\n",namalengkap);
fgets(temp,sizeof temp,fp);
sscanf(temp,"= Jenis Kelamin     : %[^\n] =\n",gender);
fgets(temp,sizeof temp,fp);
sscanf(temp,"= Umur              : %d =\n",&umur);
fgets(temp,sizeof temp,fp);
sscanf(temp,"= Tinggi Badan (CM) : %d =\n",&tb);
fgets(temp,sizeof temp,fp);
sscanf(temp,"= Berat  Badan (KG) : %d =\n,&bb)

So what's wrong in here? cause i don't think there is something wrong or maybe i didn't notice it. It would be highly appreciated if answered with the code fixed, help me a lot in learning. Thank you.

LearnerC
  • 51
  • 6
  • 2
    Ending `fscanf` string with `\n` doesn't do what you probably think it does and is usually not what you want: [Using "\n" in scanf() in C](https://stackoverflow.com/questions/15443483/using-n-in-scanf-in-c). Also, suggest you add basic error checking for each function call, specifically `fopen` and `fscanf` calls. – kaylum Jun 16 '22 at 22:11
  • `filetest4.txt`'s content doesn't match the code you've commented out. – Ted Lyngmo Jun 16 '22 at 22:14
  • @xing wow its working, thanks!, anyway is there an explanation change the width field I `fscanf` can solve the problem? – LearnerC Jun 16 '22 at 22:24
  • 2
    @LearnerC Just read about the [`scanf`](https://en.cppreference.com/w/c/io/fscanf) format strings. I don't think you'll find `-` in there. A good compiler with the warning options turned on will tell you about the invalid format string. – Ted Lyngmo Jun 16 '22 at 22:26

0 Answers0