File Reading and storing to 1 variable

This is a discussion on File Reading and storing to 1 variable within the C Programming forums, part of the General Programming Boards category; ok i think i got further this time Code: fseek(fal, 0, SEEK_END); pos = ftell(fal); fseek(fal, 0, SEEK_SET); fread(&ReadTxt , ...

  1. #16
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    ok i think i got further this time

    Code:
    		fseek(fal, 0, SEEK_END);
    		pos = ftell(fal);
    		fseek(fal, 0, SEEK_SET);
    		fread(&ReadTxt , pos , 1 , fal);
    		_stprintf(testt , _T("%d") , pos);
    		SendMessage(hwndList , WM_SETTEXT , (WPARAM)sizeof(ReadTxt) , (WPARAM)ReadTxt);
    		MessageBox(0 , testt , _T("test") , 0);
    the messagebox is showing the correct size but its setting the text as all wide chars

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > SendMessage(hwndList , WM_SETTEXT , (WPARAM)sizeof(ReadTxt) , (WPARAM)ReadTxt);
    SendMessage(hwndList , WM_SETTEXT , (WPARAM)pos , (WPARAM)ReadTxt);

  3. #18
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    still not working

  4. #19
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    also i had 2 WPARAM's i changed it to

    Code:
    SendMessage(hwndList , WM_SETTEXT , (WPARAM)pos , (LPARAM)ReadTxt);
    and still no luck

  5. #20
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    try this:
    *((char *) (rand()) = 0;

    then run it until it works

    e: oh well

    char stuff[124124];
    int pos;
    FILE * file = open me a file;

    fread(stuff, 124124, 1, file);
    fseek(file, 0, seek_end);
    pos = ftell(file);
    stuff[pos] = 0;

    printf(stuff);
    Last edited by ggs; 07-12-2004 at 02:22 PM. Reason: optimization
    .sect signature

  6. #21
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    i dont think that will help.

    this program works fine in C but soon as its with unicode it goes nuts

    Code:
                    int pos;
    		TCHAR SaveTxt[2048];
    		TCHAR ReadTxt[128];
    		TCHAR testt[128];
    		FILE *fal;
    
    		fal = fopen("users.txt" , "rw");
    
    		if(fal == NULL){
    			MessageBox(0 , _T("list not found") , _T("Error") , 0);
    		}
    
    		fseek(fal, 0, SEEK_END);
    		pos = ftell(fal);
    		fseek(fal, 0, SEEK_SET);
    		fread(&ReadTxt , pos , 1 , fal);
    		_stprintf(testt , _T("%d") , pos);
    		SendMessage(hwndList , WM_SETTEXT , (WPARAM)pos , (LPARAM)ReadTxt);
    		MessageBox(0 , testt , _T("test") , 0);
    thats all of it.
    its like its showing the buffer for ReadTxt because if i change the Arraysize on Readtxt say to 2 then it only shows 2 wierd widechar characters but if i keep it t hat big it shows a whole lot of it

  7. #22
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    fread(addresstoreadto, sizeofrecord, numberofrecords, file)

    that is to say, stop using windows
    Last edited by ggs; 07-12-2004 at 02:26 PM.
    .sect signature

  8. #23
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    fread(addresstoreadto, sizeofrecord, numberofrecords, file)
    ?
    i have all of that right.

  9. #24
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Rare177
    i dont quiet understand fread right is it

    fread(variable , eof , startof , file);

    ?
    No. Don't program randomly, but learn to get and read information.

    google "man fread" gives (1st answer) :
    Code:
    size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
    ptr: The address of the destination block of data.
    size: the size of an element of data
    nmemb : the number of elements of data
    stream: the stream object address.
    return: the actual number of read elements

    Say :
    Code:
    char a[123];
    size_t read_nb = fread (a, 1, 123, fp);
    1 : means one char
    123 : means 123 / 1 elements

    Could have been written:
    Code:
    char a[123];
    size_t read_nb = fread (a, sizeof *a, sizeof a / sizeof *a, fp);
    Emmanuel Delahaye

    "C is a sharp tool"

  10. #25
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    Quote Originally Posted by Rare177
    ?
    i have all of that right.
    very sorry, i didn't read your snippet until after i posted
    .sect signature

  11. #26
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    i understand how it works now but here ill show u something.

    //////////////////////////////
    TCHAR ReadTxt[128];
    comesup with

    獤獦㈴㈲婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚 婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚婚 婚婚婚婚婚婚婚婚婚婚婚婚婚婚

    ///////////////////////////////

    TCHAR ReadTxt[2048];
    comes up with

    ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ??????????????????????????
    ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ????????????????????
    ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ??????????????????????????????????
    ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ??????????????????

    its like the buffer is becoming the variable data

  12. #27
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Rare177
    i understand how it works now but here ill show u something.

    //////////////////////////////
    TCHAR ReadTxt[128];
    comesup with

    獤獦㈴㈲婚婚<...>

    ///////////////////////////////

    TCHAR ReadTxt[2048];
    comes up with

    ??????<...>

    its like the buffer is becoming the variable data
    Please don't copy and paste long strings of random data...

    The value of a variable defined in a bloc is undefined.

    Try this:
    Code:
    TCHAR ReadTxt[2048] = {0};
    It makes things clear. (Filled up with 0's).
    Emmanuel Delahaye

    "C is a sharp tool"

  13. #28
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    sorry about the long strings.

    i think that helped abit becuase now instead of getting heap of 2048 chars i got 獤獦㈴㈲

    but its still not working

  14. #29
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    im going to get some rest and will work on it when i get up ,
    thanks every one for the help

  15. #30
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Quote Originally Posted by ggs
    t_fpos position;
    fseek(file, 0, seek_end); // end of file
    position = ftell(file); // grab position
    fseek(file, 0, seek_set); // back to start
    ftell() does not return t_fpos, it returns a long.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  2. reading file and storing to arrays
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 05:17 AM
  3. Reading all the numbes from a file and storing in an array
    By derek tims in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2006, 03:01 PM
  4. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  5. Reading strings from a file and storing into an array
    By Rizage in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2002, 03:04 AM

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