Thread: Beginner with C: Problem with textual files

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    15

    Beginner with C: Problem with textual files

    Hello,
    I'm trying to write a program for my assignment. I'm using STRUCTS and fprintf for writing in a file. Problem: I have main screen with choices, like, create (text file with student info), append / add - (not yet started working on it), delete student(s) - (not yet started working on it) and list students from certain file - (not yet started working on it). In create, I'm passing a size of STRUCT array to function, and in function, everything works, but I can't seem to implement anything that will check if the file already exists. I'm using fgets for user input for file name and I'd really appreciate if someone could help me around checking problem. I've tried access one, it either gives me "File exists!" all the time, or lets me in loop and then breaks after 1st input (even tho I don't have any break in loop).

    Thank you for your time and help, 574515.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    15
    STURCT:
    Code:
    typedef struct date_Of_Birth {
        short int day;
        short int month;
        short int year;
    }DATE;
    
    typedef struct student {
        int pin;
        char fname[30];
        char lname[30];
        DATE date;
        float gpa;
    }STUDENT;
    Code:
    void create_List(int size) {
    
        STUDENT* p_Stu = (STUDENT*)calloc(size, sizeof(STUDENT)); //struct pointer
        if (p_Stu == NULL)
            fail();
    
        char n_F_Name[200];
        int i, len = strlen(n_F_Name);
    
        FILE* fCreate;
        fCreate = NULL;
    
        printf("Enter a name for a file: ");
        fgets(n_F_Name, 200, stdin);
    
        /*for fail
    
            printf("File with given name already exists.\n");
            system("pause");
            system("cls");
            choice_Of(1);
            fclose(fCreate);
        for success
            strcat(n_F_Name, ".txt");
            fCreate = fopen(n_F_Name, "w");
            if (fCreate == NULL)
                fail();*/
    
            for (i = 0; i < size; i++)
    //long for loop for input, works without file check
    
        fclose(fCreate);
        free(p_Stu);
    }

  4. #4
    Registered User
    Join Date
    Mar 2020
    Posts
    15
    Found the solution! Thank you for taking your time and looking here!

    Code:
    if (fCreate = fopen(e_F_Name, "r") == NULL) {
        printf("File with given name already exists.\n");
        system("pause");
        system("cls");
        choice_Of(1);
        fclose(fCreate);
    }
        else {
    //does the input

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yeah, that's no answer.

    With the operator precedence you have, fCreate ends up as 0 or 1, neither of which are good things to pass to fclose.
    if (fCreate = ( fopen(e_F_Name, "r") == NULL ) )


    Did you get an error message like this when you compiled?
    Code:
    bar.c:5:11: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
       if ( fp = fopen("foo","r") == NULL) {
               ^
    I hope you're not developing bad habits of just ignoring warnings.

    To make it work as valid code, you need explicit parentheses
    Code:
    if ( (fCreate = fopen(e_F_Name, "r")) != NULL) {
      // file exists
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2020
    Posts
    15
    Quote Originally Posted by Salem View Post
    Did you get an error message like this when you compiled?
    Code:
    bar.c:5:11: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
       if ( fp = fopen("foo","r") == NULL) {
               ^
    No, I did not. I haven't gotten any errors. I'm using Visual Studio 2019. And I do check for errors and warning messages. The only warning message I got was that types differ, since fopen returns int and I'm checking with FILE* pointer. So, as I said, still a beginner, still not sure how things should or do work. Thank you for your tips.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by 574515
    No, I did not. I haven't gotten any errors. I'm using Visual Studio 2019. And I do check for errors and warning messages. The only warning message I got was that types differ, since fopen returns int and I'm checking with FILE* pointer.
    fopen returns a FILE*, not an int: the int is because the result of fopen("foo","r") == NULL is an int. That warning message is exactly what Salem was talking about.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2020
    Posts
    15
    I've tried changing it and it was as Salem said. Well, I'm still learning, and our courses are going slowly. We haven't had any proper assignments yet, so I'm "winging" it on my own. Thank you for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Textual Analysis
    By Edwin Galloway in forum C Programming
    Replies: 5
    Last Post: 02-11-2014, 08:24 PM
  2. Beginner: I/O using .txt files
    By RyanLeonard in forum C Programming
    Replies: 7
    Last Post: 10-25-2010, 05:26 PM
  3. C Beginner with files program problem!
    By harshalizee in forum C Programming
    Replies: 1
    Last Post: 11-22-2009, 03:42 AM
  4. Removing comments from textual files
    By Micko in forum C++ Programming
    Replies: 19
    Last Post: 08-09-2006, 09:36 AM
  5. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM

Tags for this Thread