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. :)

Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
user2364091
  • 29
  • 1
  • 4
  • Do you need to locate file and then append something to it? – vladfau May 31 '13 at 20:24
  • You need to define input from user, get user name to open file name. – lsalamon May 31 '13 at 20:27
  • You've hard-coded in a single filename (`username.txt`), which obviously means it can't be anything else. If you have the user's name, use it in the filename instead of the hardcoded value; that shouldn't have been too hard for you to figure out on your own. – Ken White May 31 '13 at 20:32

4 Answers4

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/

Aurus
  • 705
  • 9
  • 21
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.

KrisSodroski
  • 2,796
  • 3
  • 24
  • 39
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

A4L
  • 17,353
  • 6
  • 49
  • 70
  • We have a winner! This was the missing link. Thank you so much for helping. I needed to = NULL for my report_file and add another char for the userfile (which I have as LINESIZE, which is defined as 200). – user2364091 May 31 '13 at 22:18
  • 1
    what happens if username is larger than `250`? – jfs May 31 '13 at 22:51
  • 1
    @J.F.Sebastian then you take pity on the soul who has to enter in a 250 character username – im so confused May 31 '13 at 22:53
  • @A4L could I trouble you on how to add the file ending? now it creates the file as for example "Bob" instead of "Bob.txt" with the ending included. Obviously, the objective has been achived so not a big deal. – user2364091 May 31 '13 at 22:56
  • 2
    @J.F.Sebastian: What about me? my username is 516 bytes of NOPs and then a rough estimate of the address of somewhere in the middle of the stack, then shellcode for hello world. – Wug May 31 '13 at 23:08
  • @user2364091 The ending or extension `.txt` is added using `sprintf` see the code I posted. It uses `username` for storing the input of the user, then it uses `userfile` (witch length has to be a least length of username + 4) to store the filename. – A4L Jun 01 '13 at 00:04
  • @A4L yes, I have that exact code and it works to create the report file of the quiz. No worries there, and thank you kindly once more. I'm not sure if this is a NetBeans thing or a MAC computer thing but after creating the correct type of file there is no .txt extension. For a MAC this just creates the type of file correctly. I've noticed this in my personal life on a MAC say for printing to .pdf in a word document the default is not to include the file extension and then you can tick a box where it will include it (just as an example). – user2364091 Jun 01 '13 at 08:50
  • @user2364091 Hmmm interesting how mac behaves, I though extension were just hidden if you brows the files the file explorer just like windows does (and witch you can turn off and have all extension shown). – A4L Jun 01 '13 at 12:21
  • @A4L nope, files on my hard drive are distinguisable by file extension i.e. .pdf or .jpg. For this program, I read a file first of questions.txt (all visible) yet this created report file is very much indeed less the file extension ending. Will ask my friend, if the behaviour is different on linux and also try later on myself on a Windows PC. Cheers. :) – user2364091 Jun 01 '13 at 13:59
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
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670