Sample code please help!!!

This is a discussion on Sample code please help!!! within the C Programming forums, part of the General Programming Boards category; THis is some code I was writing to open a txt file and read the numbers in. It compiles fine ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Sample code please help!!!

    THis is some code I was writing to open a txt file and read the numbers in. It compiles fine but when I run it the console only comes up for a split second and then disappears again. If anyone wants to look over it and post back a solution it would be greatly appreciated. I am trying to read in 5 numbers form a txt file hence the f1....f5.


    Code:
    /* Reading formatted file data with fscanf(). */
    #include<stdlib.h>
    #include<stdio.h>
    
    int main(void)
    {
        float f1,f2,f3,f4,f5;
        FILE *fp;
        
        if ((fp=fopen("input.txt", "r"))==NULL)
        {
                                   fprintf(stderr, "Error opening the file.\n");
                                   exit(1);
                                   }
        fscanf(fp, "&#37;f %f %f %f %f", &f1,&f2,&f3,&f4,&f5);
        printf(" The values are %f,%f,%f,%f, and %f\n.", f1,f2,f3,f4,f5);
        getchar();
        fclose(fp);
        return 0;}

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Try to add a 'getchar()' in the error handling section (between fprintf(stderr,...) and exit(1)), you're probably executing your program at a location where it cannot find 'input.txt' so it outputs the error, exits (and windows closes the console immediately).

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Assuming your on windows, to see what your program actually does, just simply open a command prompt and execute your program through the command prompt.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    11
    ok thanks i'll try moving the getchar(). I would execute it through the command prompt except im coding in school where they don't allow students to access the command prompt.

  5. #5
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    Why do people be that mean and disable the command prompt, its not like you can use it to overthrow the world. They would have probably disabled task manager as well, and there would be a large increase in the number of programs crashing just to despite the user.
    Also, if you were reading any more than 5 floats, then you would want to use an array, otherwise it would get to repetitive.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    11
    Moving the getchar(); worked. Thanks for the help

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by aewing30 View Post
    Moving the getchar(); worked. Thanks for the help
    you shouldn't move it, it should be in both places.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pre-Interviewer asking sample code
    By jayee_spicyguy in forum C Programming
    Replies: 3
    Last Post: 02-09-2009, 10:21 AM
  2. Quick help on bool or while? Sample code...
    By Striph in forum C Programming
    Replies: 5
    Last Post: 01-12-2006, 03:05 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. stricmp() where can I view the sample code?
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 06-28-2002, 08:40 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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