0

I have successfully created a quiz program that first askes the user to input their name.

After the quiz is over, the score is first printed to screen and then would be printed into a username.txt file.

Meaning, the actual users name like John.txt or Amy.txt or bob.txt.

Right now, I have predefined

report_file = fopen ("username.txt","w");

This works 100% yet naturally I have failed to properly identify the files each time the quiz is run for different people and recorded into the file.

I've seen suggestions for sprintf, snprintf, ofstream, istream, and the like.

All are new concepts to me and have no clue how to move forward...any help would be greatly appreciated.

If anyone wants or cares to see my program it is quiet nice and I'd be happy to share but do not intend to burden anyone to read my entire program during this ask for help. :)

4

4 回答 4

0

I've seen suggestions for sprintf, snprintf, ofstream, istream, and the like.

So why dont use them?

char buffer [256];
sprintf(buffer, "%s.txt", username);
report_file = fopen (buffer,"w");

username is obviously an zero terminated string which contains the name of the user

EDIT: For further information you can visit http://www.cplusplus.com/reference/cstdio/sprintf/

于 2013-05-31T20:28:13.567 回答
0

If you have their username, just save it into a char array, and then call open with O_CREAT set. This will create the file if its not created, and since you have their username, it will create a file with their username.

于 2013-05-31T20:30:06.483 回答
0

Your are not using "username.txt" for all users I hope. And what do you do with the name a user enters when he start the quiz?

You might need something like that in your code, see the docs for sprintf

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

int main(int argc, char** argv)  
{ 
    FILE* pf = NULL;
    char username[250];
    char userfile[255];
    printf("username: ");
    scanf("%s", username);
    sprintf(userfile, "%s.txt", username);
    pf = fopen(userfile, "w");
    fprintf(pf, "%d", 100);
    fclose(pf);

    return 0;  
} 

You might also want to have a look at fopen to see what flags for opening you will need for your requirements

于 2013-05-31T20:30:56.413 回答
0

To create the file name from a user name, you could use snprintf():

#include <stdio.h>
#include <stdlib.h>
/* ... */

const char* filename_format = "%s.txt";
int n = -1;
char* filename = NULL;

if ((n = snprintf(NULL, 0, filename_format, username)) < 0 ||
    (filename = malloc(n+1)) == NULL ||
    snprintf(filename, n+1, filename_format, username) != n) {
  free(filename);
  return error; 
}
/* use filename here... */
free(filename); /* clean up */

where username is a '\0'-terminated C string e.g., you could get it using readline():

#include <readline/readline.h> /* readline or editline, etc libraries */
/* ... */

char* username = NULL;
if ((username = readline("Enter your name: ")) == NULL) return error;
/* use username here... */
free(username); /* clean up */

To compile and link:

$ gcc -std=c99 your_source.c -o program-name -lreadline
于 2013-05-31T22:50:13.850 回答