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

file i/o problem

Options
  • 02-07-2004 11:28am
    #1
    Registered Users Posts: 1,366 ✭✭✭


    file I/O problem, The data is written to file correctly, but not read back correctly:

    int store_data(char* path)
    {
    FILE *out;
    out = fopen(path, "w");
    if(out == null)
    {
    //display error messagebox
    }

    fprintf(out, "%d", time); //time is a global variable

    fclose(out);
    }


    int read_data(char* path)
    {
    FILE *in;
    in = fopen(path, "r");
    if(in == null)
    {
    //display error messagebox
    }

    fscanf(in, "%d", &new_variable); //new variable is a global variable

    fclose(in);

    }


    Ive simplified the code a good bit, but this is basically what I'm doing.

    The new_variable is not being set, even tho the file is written to correctly..

    anyone spot a mistake?

    Thanks,
    king


Comments

  • Closed Accounts Posts: 3,322 ✭✭✭Repli


    Post up where you call the functions from main()
    tbh i find i/o a lot easier in C++ - can post up some code to do same thing if u like

    Just noticed something - you arent returning ints in your functions


  • Registered Users Posts: 1,366 ✭✭✭king_of_inismac


    The application is win32.

    The functions are called from MainWndProc

    code goes like this:

    switch(msg) {

    switch(LOWORD(wParam))
    {
    case IDC_FILE_SAVE:

    r = save_as(hwnd);
    break;
    case IDC_FILE_OPEN:
    r = open_file(hwnd);
    break; .....
    }

    got the following two functions off the net. They basically pop up the proper dialogs and get the filenames, which are passed to the functions described in my first post:

    int save_as(HWND main_window)
    {
    char t_text[2056];
    OPENFILENAME t_save_file;

    memset(&t_text, 0, sizeof(t_text));
    memset(&t_save_file, 0, sizeof(OPENFILENAME));
    t_save_file.lStructSize = sizeof(OPENFILENAME);
    t_save_file.hwndOwner = main_window;
    t_save_file.lpstrFilter = FILTER;
    t_save_file.lpstrFile = g_long_filename;
    t_save_file.nMaxFile = sizeof(g_long_filename);
    t_save_file.lpstrFileTitle = g_short_filename;
    t_save_file.nMaxFileTitle = sizeof(g_short_filename);
    t_save_file.Flags = OFN_CREATEPROMPT | OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
    t_save_file.lpstrDefExt = DEF_EXTENSION;
    if(0 == GetSaveFileName(&t_save_file)) {
    if(0 != CommDlgExtendedError())
    return -1;
    else
    return 1;
    }

    memset(t_text, 0, sizeof(t_text));

    //heres where I call my function

    store_data(g_long_filename);


    return 0;
    }

    int open_file(HWND main_window)
    {
    char t_text[2000];
    OPENFILENAME t_open_file;

    memset(&t_text, 0, sizeof(t_text));
    memset(&t_open_file, 0, sizeof(OPENFILENAME));
    t_open_file.lStructSize = sizeof(OPENFILENAME);
    t_open_file.hwndOwner = main_window;
    t_open_file.lpstrFilter = FILTER;
    t_open_file.lpstrFile = g_long_filename;
    t_open_file.nMaxFile = sizeof(g_long_filename);
    t_open_file.lpstrFileTitle = g_short_filename;
    t_open_file.nMaxFileTitle = sizeof(g_short_filename);
    t_open_file.Flags = OFN_CREATEPROMPT | OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST;
    t_open_file.lpstrDefExt = DEF_EXTENSION;
    if(0 == GetOpenFileName(&t_open_file)) {
    if(0 != CommDlgExtendedError())
    return -1;
    else
    return 1;
    }

    memset(t_text, 0, sizeof(t_text));

    //heres where I call my function
    read_file(g_long_filename);


    return 0;
    }

    ideas?

    ps: about the ints, I know, am a lazy git, just want to get the thing running, don't think it makes a difference?


  • Closed Accounts Posts: 3,322 ✭✭✭Repli


    Ah - I dont use WinAPI - much prefer MFC
    anyway try changing the return types of your functions to voids, or else use this code:

    [php]
    void store_data ( char *path )
    {
    ofstream out( path );

    if ( out.fail() )
    // error message

    out << time;

    out.close();
    }

    void read_data ( char *path )
    {
    ifstream in( path );

    if ( in.fail() )
    // error message

    in >> new_variable;

    in.close();
    }
    [/php]


  • Registered Users Posts: 1,366 ✭✭✭king_of_inismac


    Thanks man,

    Ya prefer MFCs myself, but I'm working with soem *redundant* software. :D

    BTW,What library do I need to include to use those functions?


  • Closed Accounts Posts: 3,322 ✭✭✭Repli


    You will need:
    #include <fstream>

    and probably
    #include <iostream>


  • Advertisement
  • Registered Users Posts: 1,366 ✭✭✭king_of_inismac


    Thanks Repli,

    Would still like o know what the problem is with the C code,

    Has anyone else any ideas?

    Thanks, king


Advertisement