Hi, I got a problem in reading and write on file.
I have to create an virtual agenda, therefore I enter a name and a number for N persons.
After entering the infos if the file doesn't exist, the program create it and save the infos, if the file exist the program will overwrite all the lines.

But when i write on screen the information from a fprintf the lines are messed up.

I load the infos form the file with this:

    fp=fopen("agenda.txt","r");
            if(fp)
            {
                rewind(fp);
                while(!feof(fp))
                {
                    fscanf(fp,"%s %s",inome,inum);
                    if(ii==NULL)
                    {
                        head=malloc(sizeof(agenda));
                        if(!head)
                        {
                            printf("Memory Problems");
                            exit(1);
                        }
                        head->prev=NULL;
                        head->next=NULL;
                        strcpy(head->nome,inome);
                        strcpy(head->num,inum);
                        ii=head;
                        fflush(stdin);
                    }

And i save the info from the memory to the file with this

void save()
{
    fp=fopen("agenda.txt","w");
    ptr ii=head;
    rewind(fp);
    while(ii)
    {
        fprintf(fp,"%s %s",ii->nome,ii->num);
        ii=ii->next;
    }
    fclose(fp);
    return;
}

This is what happens after that the program writes on screen the infos from the file

Dani AI

Generated

Brief diagnosis and concrete fixes for 's thread (builds on comments from and others):

The visible symptom (records running together) is almost certainly a missing line terminator when saving plus fragile parsing and list construction when loading. correctly called out the newline issue; the posted use of fflush(stdin) is undefined behavior, and while(!feof(fp)) with fscanf("%s %s",...) is fragile (it fails for names with spaces and misdetects EOF). Also the loader only allocates the head node and never appends a new node for each record, which explains corrupted/incomplete list state that follows.

Safe, practical changes (examples follow):

  • Write one logical record per line and include a newline. Use snprintf + fputs or fprintf that ends with \n.
  • Read full lines with fgets() and parse the line (tab or another delimiter is safer than whitespace tokenization). Check the parse function return value before using data.
  • Initialize head/tail/iterator pointers to NULL before loading and allocate a new node for every parsed line; link next/prev correctly and update the tail.

Example patterns (illustrative; adapt field sizes and types):

/* append record safely */
FILE *fp = fopen("agenda.txt", "a");
char buf[256];
snprintf(buf, sizeof buf, "%s\t%s\n", name_buf, num_buf);
fputs(buf, fp);
fclose(fp);
/* read line-by-line and parse */
char line[256];
while (fgets(line, sizeof line, fp)) {
    char name[128], num[64];
    if (sscanf(line, "%127[^\t]\t%63s", name, num) != 2) continue;
    /* allocate node, copy safely, link into list */
}

Checklist / cautions:

  • Check return values for fopen, fgets, malloc, sscanf.
  • Avoid fflush(stdin); use fgets for console input or consume leftover chars with a getchar loop.
  • Use bounded string functions (snprintf, strncpy/strlcpy) to prevent overruns.
  • Decide whether files should be overwritten ("w") or appended ("a") and open accordingly.

These changes address the line-termination, input-parsing, and linked-list allocation issues noted in the replies from and .

Recommended Answers

All 4 Replies

fprintf(fp,"%s %s",ii->nome,ii->num);

The problem is that you have to put '\n' at the end. And your program won't work at all if you put a space in the name that you enter, such as "John Smith" or something like that.
fprintf(fp,"%s %s\n",ii->nome,ii->num);

ur program is incomplete plz upload ur full program

I feel that it should work fine but some problem is in memory allocation

I feel that it should work fine but some problem is in memory allocation

Contridction! if there is a prblem then it is not working find :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.