Thread: help with fopen() and strings. i'm a noob.

  1. #1
    Registered User
    Join Date
    Dec 2005

    help with fopen() and strings. i'm a noob.

    i'm coding a game program for my c class and i need some help.

    okay, at the beginning of the game it reads the high scores from a .txt file and prints it to the screen. the standard file is called scores.txt, so it would look kind of like this:

    inp = fopen("scores.txt", "r");

    fscanf(inp, "%d%d%d%d", &score1, &score2, &score3, &score4);

    printf("Best Scores\t%d\t%d\t%d\t%d", score1, score2, score3, score4);


    but i am supposed to give the option of changing the score file. I was thinking of doing this with strings, for example:

    inp = fopen("string_name.txt", "r");

    i tried doing someting like:

    char string_name[] = "scores";
    inp = fopen("%s.txt", string_name "r");

    and then another function to redefine string_name[] with whatever filename the user chooses, but it says theirs too many arguments or something.

    Is there a way to use strings as the filename in fopen()? or is there another simple way i can achieve this?

    thank you.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    char filename[SIZE];
    FILE *fp = fopen(filename, "rt");
    /* first10lines */
    #include <stdio.h>
    #include <string.h>
    int main(void) {
        FILE *fp;
        char fn[100], line[80];
        int x;
        printf("\nEnter a file to view: ");
        fgets(fn, sizeof(fn), stdin);
        if(fn[strlen(fn)-1] == '\n') fn[strlen(fn)-1] = 0;
        if((fp=fopen(fn, "rt")) == NULL) {
            printf("Can't open that file\n");
            return 1;
        for(x = 0; x < 10 && fgets(line, sizeof(line), fp); x ++) {
            printf("%s", line);
        return 0;
    Last edited by dwks; 12-04-2005 at 03:54 PM.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2005
    got it, thanks alot for your help!

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    if(fn[strlen(fn)-1] == '\n') fn[strlen(fn)-1] = 0;
    No need to traverse the string twice; other variations that don't work fine.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question: Stream I/O (working with strings)
    By ckuttruff in forum C Programming
    Replies: 15
    Last Post: 05-19-2008, 11:32 AM
  2. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  3. Join 2 strings inside 'fopen'?
    By tomas.ra in forum C Programming
    Replies: 5
    Last Post: 09-24-2005, 04:08 PM
  4. Inputting strings from a file
    By Mystic_Skies in forum C Programming
    Replies: 7
    Last Post: 11-17-2004, 03:08 PM
  5. problem with fopen command
    By emon in forum C Programming
    Replies: 2
    Last Post: 03-11-2004, 11:11 PM
Website Security Test