Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C Program to read one line from another file

  • 11-11-2011 10:06am
    #1
    Closed Accounts Posts: 95 ✭✭


    Basically I have to make a guessing game where the answers and hints are stored in another file and the program reads them from there. However, before I even get halfway into it I want to be sure I'm storing the first string correctly.

    When I put this code in I'm looking for it to read the first line, which will be the guess, but the code won't compile.

    Not sure if it makes sense to write a little bit of my program first then try with the rest but if I don't get this then I can't see how I'll get the rest.
    #include<stdio.h> 
    int main()
    {
    FILE *fp, *fp1, *fprint;
    int letter;
    char fname[80];
    char guess[80];
    printf("Please enter the name of the file you wish to print: ");
    gets(fname);
    fp=fopen (fname, "r");
    letter = fgetc(fp);
    printf("The contents of the file <%s> are:\n", fname);
    	while (letter != '\n')
    	{
    	fgets(guess, 80, fp1);
    	letter=fgetc(fp);
    	}
    
    printf("%s", guess);
    return 0;
    }
    

    It tells me permission is denied for the output file.


Comments

  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    It makes sense to break things down as you said!

    Looking at that code, it doesn't look like you've opened the fp1 file handle but you're calling fgets on it.

    Plus, if you want to write to a file, you have to use the fputs funtion. How about trying to write to a file first, then adding the put to read from another file after?


  • Closed Accounts Posts: 95 ✭✭The Crab


    It makes sense to break things down as you said!

    Looking at that code, it doesn't look like you've opened the fp1 file handle but you're calling fgets on it.

    Plus, if you want to write to a file, you have to use the fputs funtion. How about trying to write to a file first, then adding the put to read from another file after?

    I don't need to write to a file. I've already done some of those in this assignment. I probably worded the question badly.

    I'll have a look at the fp1 issue, thanks!


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    Ah I get it, you need to read a single line from the file!

    If you're only dealing with one file, get rid of fp1 and fprint handles. It'll make your code more readable and it's good practice.

    So yeah, if you change the fp1 in your while loop you'll make a bit more progress, but I think there's still a flaw in your logic but I'll leave that up to you to figure out for now!


  • Closed Accounts Posts: 95 ✭✭The Crab


    Ah I get it, you need to read a single line from the file!

    If you're only dealing with one file, get rid of fp1 and fprint handles. It'll make your code more readable and it's good practice.

    So yeah, if you change the fp1 in your while loop you'll make a bit more progress, but I think there's still a flaw in your logic but I'll leave that up to you to figure out for now!

    Is it that I have to do that stuff with converting chars to ints and so on??? Please please tell me that it isn't. I tried that last week and couldn't get my head around it.


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    No, it's not that.

    When you call fgetc or fgets, you advance the position of the file stream. So by searching for an EOL character, you're not at the beginning of the file any more!

    Also, there's an fscanf function that you could use too, but try it your way first!


  • Advertisement
  • Closed Accounts Posts: 95 ✭✭The Crab


    No, it's not that.

    When you call fgetc or fgets, you advance the position of the file stream. So by searching for an EOL character, you're not at the beginning of the file any more!

    Also, there's an fscanf function that you could use too, but try it your way first!

    Thanks, is my logic at least anyway right? I find C such a struggle... :(


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    C is pretty hard, but scanning through files like this and getting your head around it is fundamental stuff worth it. It's relevant regardless of language and something a lot of programmers these days don't know about!

    Your logic is fine, but if you call these functions, you'll need to reset the file to read the first line.


  • Closed Accounts Posts: 95 ✭✭The Crab


    C is pretty hard, but scanning through files like this and getting your head around it is fundamental stuff worth it. It's relevant regardless of language and something a lot of programmers these days don't know about!

    Your logic is fine, but if you call these functions, you'll need to reset the file to read the first line.

    How can I find out how to do that? I'm certain my lecturers didn't tell me.


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    The functions you're using are part of a C library called stdio.

    Here is some documentation on it. Ignore the fact that it's on a C++ website, C++ is a not so strict superset of C.


Advertisement