0

I'm trying to make a program that can save records/notes that can be written and read with the program. But I was wondering why the .txt files were always jumbled up with weird symbols.

struct record {
    char time[6];
    char name[30];
    char place[25];
    char note[500];
} ;

void addrecord() {
    system("cls");
    FILE *fp ;
    char another = 'Y';
    char time[10];
    struct record diary ;
    char filename[15];

    printf("\n\n\t\t***************************\n");
    printf("\t\t* MENU TO ADD A RECORD *");
    printf("\n\t\t***************************\n\n");
    printf("\n\n\tDATE OF RECORD:[yyyy-mm-dd]:");
    scanf("%s", filename);

    fp = fopen (filename, "a+" ) ;
    if ( fp == NULL ) {
        fp=fopen(filename,"w+");
        if(fp==NULL) {
            printf("\nPROGRAM ERROR!");
            printf("\nPRESS ANY KEY TO TRY AGAIN!");
            getch();
            return ;
        }
    }
    printf ( "\n\tTIME OF RECORD:[hh:mm]:");
    scanf("%s",time);
    while(fread(&diary,sizeof(diary),1,fp)==1) {
        if(strcmp(diary.time,time)==0) {
            printf("\n\tRECORD ALREADY EXISTS!.\n");
            return;
        }
    }
    strcpy(diary.time,time);

    printf("\tNAME:");
    scanf(" %[^\n]s ",diary.name);

    printf("\tPLACE:");
    scanf(" %[^\n]s ",diary.place);

    printf("\tNOTE:");
    scanf(" %[^\n]s ",diary.note);

    fwrite (&diary, sizeof(diary), 1, fp ) ;
    printf("\nRECORD HAS BEEN ADDED\n");

    fclose ( fp ) ;
    printf("\n\n\tPRESS ANY KEY TO EXIT...");
    getch();
}

The code above is the function for creating the txt files. If my input is:

filename: 2020-03-22

TIME: 22:08

NAME: BOB

PLACE: HOME

NOTE: THIS IS A TEST

The generated txt file contains the following:

22:08 BOB uÊ ˆÊ ¸Ê þa ´]‡wÿa Home ÿa ÿa Úr@ ^      THIS IS A TEST d   ÿa 8ýa ´]‡w      w 2
d€  F„u       Ûr@  ÿ   €ç‡w   ¸Y€ *Ê ùé‡w               *Ê S 1     àOÊ  *Ê À Ê ¸  =    €           (þa ¸  &   0   èþa Ÿ‹w'R—eþÿÿÿHþa /°‡w         $þa       Ê ÐK€ ÿa        þa       àY€               <þa &¤}uÊ ÙŽ[§    F„u              þa    8þa ;©}uÊ xþa RÕ}u   BÕ}uŽ[§    F„u    $      Hþa $   èþa ÀÌ~u=â¹Ò F„u           *Ê ”þa õou4D„u þa oŠu   àþa ŽS€u     F„ukS€uŽ[§Ð@ Ð@        ´þa Ž[§Ìÿa ÀÌ~u­ê¹Òþÿÿÿø

Not sure if this is what's supposed to happen... help?

rainer
  • 3,295
  • 5
  • 34
  • 50
  • 2
    For example, the `name` field has indeterminate values stored after `'B'`, `'O'`, `'B'`, and `'\0'`. `fwrite()` is writing all of the bytes of the `struct` to file, including the indeterminate bytes. – ad absurdum Mar 21 '20 at 15:20
  • 2
    Aside: `%[^\n]s` should be `%[^\n]`. Your format is a hybrid of that and `%s`. – Weather Vane Mar 21 '20 at 15:23
  • 1
    Try `struct record diary = { 0 } ;` with initialisation. – Weather Vane Mar 21 '20 at 15:24
  • The contents of `diary` are initially undefined. You then store some strings into the arrays in `diary`, but everything after the terminating `'\0'` characters remain undefined. The file reflects the values that are actually present. If you don't like having undefined values, you can call `bzero` to zero the entire structure before using it. – Tom Karzes Mar 21 '20 at 15:26
  • what do you mean calling bzero? I'll try initializing the struct, thanks for the comments! – Harry Purba Mar 21 '20 at 15:33
  • initializing the struct works! Thanks a bunch! – Harry Purba Mar 21 '20 at 15:36
  • 1
    Note there are still a whole bunch of unprintable nul characters in between your text, but your editor probably doesn't show them. If you view the file with a hex editor you'll see them. That's not necessarily a problem, but you shouldn't think of this file as a "text file", and it would be misleading to name it `.txt`. – Nate Eldredge Mar 21 '20 at 15:39
  • I just found out that fwrite and fread is for binary files. That could be the reason why .txt doesn't work too well with it. Thanks for the info! – Harry Purba Mar 21 '20 at 15:49
  • 1
    @HarryPurba `bzero` is just a way to store `0` to the entire structure, leaving it in a well-defined state. Look at the man page for it. – Tom Karzes Mar 21 '20 at 16:13
  • 1
    @TomKarzes -- `bzero()` _was_ a POSIX function, but never part of Standard C. It has since been deprecated, and finally _removed_ from the POSIX specification. [Why use `bzero()` instead of `memset()`, anyway?](https://stackoverflow.com/questions/17096990/why-use-bzero-over-memset) – ad absurdum Mar 21 '20 at 18:02
  • @exnihilo Ok, so use memset then. – Tom Karzes Mar 21 '20 at 18:45

0 Answers0