fopen("FILE_IN.TXT", "r") problems

This is a discussion on fopen("FILE_IN.TXT", "r") problems within the C Programming forums, part of the General Programming Boards category; I am compiling with Borland Turbo C++ v3.0 and creating a small prog for a college project but I'm having ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    3

    Unhappy fopen("FILE_IN.TXT", "r") problems

    I am compiling with Borland Turbo C++ v3.0 and creating a small prog for a college project but I'm having problems with syntax.

    I had a functional prog which used elements of an array to display a "Sales League" ie. ID, NAME, SALES.

    (I'm sorry but I just can't get the spacing right on this forum page)
    ------------------------------------------------------------------------------------

    typedef struct {
    int id;
    char name[MAX_SALES_NAME + 1];
    double sales_total;
    } sales_struct;

    sales_struct sales_array[] = { {111, "Kani Beetit", 0 },
    {122, "Selyer Grahni", 0 },
    {133, "Mayjar Rippof", 0 },
    {144, "Glesgah Barras", 0 },
    {155, "Wattah Barghin", 0 }, };

    NOTE: (Use a Scots Accent for the names !!)
    ------------------------------------------------------------------------------------

    The menu had options to update this sales figure for one or for all salespersons, display the formatted "report" on screen, and send formatted output (in append mode) to a text file (FILE_OUT) for printing.
    ------------------------------------------------------------------------------------
    Report created: Tue May 21 20:31:07 2002
    By BillD

    SALES ID SALES AGENT TOTAL
    ========= ============ ========
    111 Kani Beetit 654.87 OK
    122 Selyer Grahni 875.12 OK
    133 Mayjar Rippof 654.98 OK
    144 Glesgah Barras 564.46 ***
    155 Wattah Barghin 500.00 ***
    ========= ============ =========
    GRAND TOTAL 3249.43

    OK = SATISFACTORY FIGURES
    *** = IMMEDIATE DISMISSAL !!
    -----------------------------------------------------------------
    Instead of loading the elements of the array from elements declared within the programme, I need to load them from a basic text file (FILE_IN) as the same array previously loaded internally.

    111 Kani Beetit 0.00
    122 Selyer Grahni 0.00
    133 Mayjar Rippof 0.00
    144 Glesgah Barras 0.00
    155 Wattah Barghin 0.00

    I have tried to write a new function "load_sales_structure" which I will call immediately the programme starts with "int main()", but I have tied myself in knots trying to get the function declaration and call right. I've downloaded loads of sample code from loads of online resources, but they all seem to refer to loading from binary files and are usually shown as being a simple standalone prog. The programme compiles but obviously isn't loading data from "FILE_IN" because it shows all SALES ID's as 0, blank fields for SALES_AGENTS, but all other updates are OK.

    I really need some advice and hoped than maybe one or two of you guys could take a look at the code attached (READWTST.CPP) and let me know where I am going wrong. Sorry It's such a long posting.

    Regards BillD [CODE][CODE]
    Attached Files Attached Files

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > (I'm sorry but I just can't get the spacing right on this forum page)
    That's why code tags were invented.
    The world is waiting. I must leave you now.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    First off, if you're going to program in C, then make your source .c, not .cpp

    Code:
    static void load_sales_structure ( sales_struct sales_array[] ) {
        int entries = 0 ;
        FILE *fields;
        if ((fields = fopen("FILE_IN.TXT", "r")) == NULL) {
            printf("\n\nFILE_IN is inaccessible for some reason\n");
        } else {
            while (fscanf (fields, "%d %s %d",
                &sales_array[entries].id,
                sales_array[entries].name,
                &sales_array[entries].sales_total ) == 3) entries++;
            fclose(fields);
         }
    }
    sales_struct sales_array [5]; /**** Don't know how to declare this ****/
    It's OK, but this is better
    sales_struct sales_array [MAX_SALES_FORCE];

    Finally, to call the function, do this
    load_sales_structure( sales_array );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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