Problem with reading in file...

This is a discussion on Problem with reading in file... within the C Programming forums, part of the General Programming Boards category; Hi, I'm making a program for my CS1 class that will read in comic book names from a file, put ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    Problem with reading in file...

    Hi, I'm making a program for my CS1 class that will read in comic book names from a file, put them in a linked list, sort them, and then print them in a specific order. The problem I'm having right now is that when I run the program and type in the filename a runtime error occurs that pops up and asks me if I want to run Visual C++. I know where the problem is in my code but I don't know how to fix it. I marked where the problem is in my code below:

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    
    struct comic
    {
           char title[100];
           char issue[100];
           int issuenum;
           struct comic *next;
    };
    
    void assign(struct comic *comics, char title[100], char issue[100], int issuenum);
    
    main()
    {
          char filename[50], title[100], issue[100];
          char uscore = '_';
          char blank = ' ';
          int issuenum, numcomics, count = 0, cnt = 0;
          struct comic *thecomics;
          FILE *f1;
          
          printf("--------------------------------------------------------------------------------------");
          printf("Welcome to the comic book sorter!\n");
          printf("Please enter in the name of the file containing the comic books: ");
          
          //PROBLEM IS SOMEWHERE RIGHT IN HERE
          fflush(stdin);
          gets(filename);
          
           //Opens the file
           f1 = fopen(filename, "r");
           fflush(stdin);
           
           if(f1 != NULL)
           {
                 fscanf(f1, "%d", numcomics);
                 system("pause");
                 
                 do
                 {
                        fscanf(f1, "%s %d %s", &title, &issuenum, &issue);
                        cnt = 0;
                        //Checks for the _ in the string and replaces it with a "space"
                        do
                        {
                             if(title[cnt] == uscore)
                             {
                                     title[cnt] = blank;
                             }
                             cnt++;
                        }while(cnt < 100);
                                   
                        //Checks for the _ in the string and replaces it with a "space"
                        cnt = 0;
                        do
                        {
                             if(issue[cnt] == uscore)
                             {
                                     issue[cnt] = blank;
                             }
                             cnt++;
                        }while(cnt < 100);
                        
                        assign(thecomics, title, issue, issuenum);
                        
                        count++;
                 }while(count < numcomics);
           }//Close if
           else
           {
               printf("There is nothing in the file!");
           }//Close Else
    }//Close main
    
    void assign(struct comic *comics, char title[100], char issue[100], int issuenum)
    {
         struct comic *count = comics;
         struct comic *start = (struct comic*)malloc(sizeof(struct comic));
         
         strcpy(start->title, title);
         strcpy(start->issue, issue);
         start->issuenum = issuenum;
         start->next = NULL;
         
         while(count->next != NULL)
         {
              count = count->next;
         }
         
         if(count == NULL)
         {
              count = start;
         }
         else
         {
             count->next = start;
         }
    }
    Anyone know whats going on?

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Don't fflush stdin. There is a FAQ here that you should read. Run a search.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    Never, never, never use gets. Always use fgets. Forget that gets ever existed. Gets is the devil.
    And while we're at it, main() should actually be int main(void).
    And seeing as you have Visual C++, I also recommend the use of safe functions with an "_s" at the end, such fscanf_s.

    Code:
    fscanf(f1, "&#37;s %d %s", &title, &issuenum, &issue);
    This is wrong (and probably why you get a crash). Don't use & on arrays. When you pass an array, it automatically passes the address, so in essence, what you're doing is passing char** instead of char*.

    Code:
    fscanf(f1, "%d", numcomics);
    This is also wrong. Fscanf requires a pointer, but you are passing the variable's value instead.

    Code:
    void assign(struct comic *comics, char title[100], char issue[100], int issuenum)
    You have a problem here, too. Since you are just passing a pointer to the function, you are essentially passing a pointer by value. In other words, main will never get the changes that the function does, so it's leaking the list on every call.
    What's worse is that the pointer is uninitialized, so you would get a crash when the function tries to access the member "next."
    Last edited by Elysia; 03-27-2008 at 01:14 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include<string.h> /* add */
    Code:
    fscanf(f1, "&#37;d", &numcomics); /* add & */
    Code:
    fscanf(f1, "%s %d %s", title, &issuenum, issue); /* remove a couple &'s */
    This gives me pause, but I'm briefly skimming:
    Code:
    struct comic *thecomics;
    /* ... */
    assign(thecomics, title, issue, issuenum);
    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. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21