Thread: reading file problem

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    20

    Smile reading file problem

    Hi everyone;

    i am making a program to get familiar with writing and reading files,this program is to write the time that lets say an employee comes to work and what time she checks out ,and only executes twice a day.

    i have a problem with my code ,
    i want to create a file and to write to it sequentially.
    the info to write is the date format and local time,that it will write to file every time we execute the program in WM_CREATE msg.
    ///////
    //////
    static int ,i, j, k, l;

    static char a[50], s[50],

    file *pfile
    //////
    //////

    to get
    GetlocalTime(///////////////////////,, ////////////)


    GetDate//Format(//////////////////&st, a, ////////)
    Here i stored the value for date format which is like:
    "tuesday, january, 14 2003" in array a and start writing the info in file name time.dat:

    fopen = ,,,,,(pfile , "time.dat"//////);

    fprintf(pfile, "%s%d%d%d\n", a, st.wHour, st.wMinute, st.wSecond);
    fclose(pfile);
    up to here i check the file and the program writes to it Ok.
    then in WM_PAINT i process reding the same file and go about it by:

    fscanf(pfile, "%s%d%d%d", s, j, k, l);
    sprintf(f, "%d%d%d", j, k, l);

    here is the problem when i try reading the values with text out

    TextOut(//////////////,,,,,,s,,,,,)
    TextOut(///////////////,,,,f,,,,,,,)
    what it shows on screen is like;

    thursday 11.48.20
    and leaves out the rest of the date format ,but if i use 4 arrays in fscanf instead of one like this:

    fscanf(pfile, "%s%s%s%s%d%d%d", s, s1, s2, s3, j, k, l);

    it shows the whole string. WHY? is it because it reads the space characters as null how do i skip them?www.cprogramming.com http\\www.cprog.com i have a problem iiiiiiiiiiiii [IMG]http://[/IMG] i want to
    Last edited by samsam1; 01-17-2003 at 06:07 PM.

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    What the hell's going on there?

    Pleasepleasepleasepleaseplease use [code][/code] tags!
    Pretty please?
    With a cherry on top?

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    20

    Smile re;clean code

    OK im sorry.

    im simplifing for you with more detail;

    SYSTEMTIME ST;
    static char s[40], a[40],;
    FILE *pfile;


    first i created a sequential file "time.dat" then i used:


    GetDateFormat(LOCAL_SYSTEM_DEFAULT, DATE_LONGDATE, &st,NULL, s, sizeof(s));

    now the value: "wednesday, january, 15 2003"
    is stored in array s fine.
    i write it to file time.dat like this:

    pfile = fopen("time.dat", "a+");

    fprintf(pfile, "%s", s);
    fclose(pfile);

    Again i check the file its fine info appears in the file as string s.

    now i try to read it from the file as follows:

    pfile = fopen("time.dat", "r")

    fscanf(pfile, "%s", a);

    TextOut(hdc, 0, 0, a, strlen(a));

    now this time it reads it as:

    "thursday" and nothing appear after thursday.

    but if i initialize 4more arrays (a1 a2 a3 a4) and use fscanf like this:

    fscanf(pfile, "%s%s%s%s", a, a1, a2, a3);

    sprintf(a4, "%s%s%s%s", a, a1, a2, a3);

    TextOut(hdc, 0, 0, a4, strlen(a4));

    everything works fine as it should:

    "wednesday, january, 15 2003"

    i think the problem is fscanf is reading the space char as null how can i fix it so i can only be able 1 array for output?

    i hope i was neat!

  4. #4
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    no he means code tags

    Code:
    like this

  5. #5
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    Code tags aren't particularly necessary with the small chunks that he's got (at least in his seocnd post)

    try something like...(I think, anyhow - I haven't used scanf() or its variants in a while...)

    fscanf(pfile, "%[^\n]", string);

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, 02:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM