0

I'm studying C at the moment and I'm not that good yet. I am trying to write a program where I want to enter some people's name and create at the same time a .txt file with their name. For example if I type "Richard" it will create the file Richard.txt. And inside the .txt file I want to write again their name.

The only problem is that after I type the first name and create the first .txt file, entering a new name will not create a new .txt file. But instead it will put the second name in the first .txt file after the first name.

#include <stdio.h> 
#include <conio.h> 
#include <string.h>
#include <stdlib.h>

struct personnel
{
 char name[40]; 
};

int addPatient(struct personnel patient[], int noAgt);
void writeFile(struct personnel patient[], int noAgt, char filename[]);
void emptyBuffer(void);

int main()
{
    struct personnel patient[50];
    int ch = 'X';
    int noAgt = 0; 
    char filename[100];
    while (ch != 'q')
    {
    printf("\na)\tEnter new patient"
    "\nb)\tWrite file"
    "\nc)\tExit program"
    "\n\nSelect: ");
    ch = getche(); 
    printf("\n\n");
    switch (ch)
        {
        case 'a' :
        noAgt = addPatient(patient, noAgt);
        break;
        case 'b' :
        writeFile(patient, noAgt, filename);
        break;
        case 'c' :
        exit(0);
        }
    }
}

int addPatient(struct personnel patient[], int noAgt)
{
 printf("\nPatient %d.\nEnter name: ", noAgt + 1); 
 scanf("%39[^\n]", patient[noAgt].name);
 while(getchar() != '\n') 
 {
    ;
 }
 return ++noAgt;
}

void writeFile(struct personnel patient[], int noAgt, char filename[])
{
    int i;
    FILE *fptr;
    struct personnel rec;
    strcpy(filename, patient[i].name);
    emptyBuffer();
    strcat(filename, ".aow.txt");
    fptr = fopen(filename, "w");
    for(i = 0; i < noAgt; i++)
    {
        rec = patient[i];
        fprintf(fptr, "Name: %s ",  rec.name);
    }
    fclose(fptr);
    printf("\nFile of %d patients written.\n", noAgt);
}

void emptyBuffer(void) /* Empty keyboard buffer */
{
 while(getchar() != '\n')
 {
     ;
 }
}

"int addPatient(struct personnel patient[], int noAgt)" Is the bit where I enter the name of the person that goes in writeFile().

"void writeFile(struct personnel patient[], int noAgt, char filename[])" Is the bit where I write the file.

1 Answer 1

0

My first advice is: If you don't need the filename variable in main, then move it to writeFile(). Less parameters moving around and makes your code cleaner.

Your issue is inside the writeFile() function:

strcpy(filename, patient[i].name);

You never initialized your i variable. It's likely always getting initialized to 0 thus you're always writing to the first file you created. Try changing that line to this:

strcpy(filename, patient[noAgt-1].name);

And you should see the code working better. Still not the best solution in my opinion, since noAgt should probably not increment before writing the file. But it should get you going on cleaning up your code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.