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; im trying to read a full file using fscanf and storing it to 1 variable inside the file i have ...

  1. #1
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214

    File Reading and storing to 1 variable

    im trying to read a full file using fscanf and storing it to 1 variable
    inside the file i have tried i few tests like

    test test test
    test test test
    test test test

    i can only get it to read 1 of the tests.is there a simple way to read the entire file and store it to one variable?
    thanks

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    thanks thats helped a fair bit i was stuck on fscanf
    its for a win32 unicode app.
    im wondering is there a unicode function for fread?

  4. #4
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    Code:
    if(fread(&ReadTxt , sizeof(ReadTxt) , 1 , fal)){
    			if(feof(fal)){
    				break;
    			}
    		}
    fal is the file.

    i am doing this and then having a messagebox show it and its comming up with alot of messy wide chars.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >and its comming up with alot of messy wide chars.
    Did you terminate the string with '\0' before trying to print it? fread is a block transfer function, it doesn't automatically append your buffer with a null character to make it a string.
    My best code is written with the delete key.

  6. #6
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    how would i do that(sorry ive only been doing C a lil while)

    also i done it in a normal C app as

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      FILE *fp;
      char test[2048];
      fp = fopen("test.txt" , "r");
      
      
      	while(fp != feof){
        		fread(&test , sizeof(test) , 1 , fp);
        		break;
       }  
        printf("%s" , test);
        getch();
    }
    it reads the file fine but adds a few unwanted chars to the end.
    in the unicode winapi program it didnt even get one bit of it right

  7. #7
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    use fseek to seek to the end of the file
    use ftell to grab the position there
    fseek back to the beginning
    fread your entire file (it is possible that fread will set the file position no farther than the end of the file, so that earlier fseek might not be necessary)
    then set the position obtained from ftell in your buffer to 0
    .sect signature

  8. #8
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    beats me how but i accidently added a number to the arraysize on the C version and it worked

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      FILE *fp;
      char test[12048];
      fp = fopen("test.txt" , "r");
      
      
      	while(fp != feof){
        		fread(&test , sizeof(test) , 1 , fp);
        		break;
       }  
        printf("%s" , test);
        getch();
    }
    but i still cant get it to work in my unicode version(win32 api program)

  9. #9
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    use fseek to seek to the end of the file
    use ftell to grab the position there
    fseek back to the beginning
    fread your entire file (it is possible that fread will set the file position no farther than the end of the file, so that earlier fseek might not be necessary)
    then set the position obtained from ftell in your buffer to 0
    im not to sure how to use fseek or ftell

    ive looked throu google with not to much luck.
    could u maybe please make a small example or point me to a link.thanks

  10. #10
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    t_fpos position;
    fseek(file, 0, seek_end); // end of file
    position = ftell(file); // grab position
    fseek(file, 0, seek_set); // back to start

    i might have screwed up the parameters of fseek
    also, t_fpos may or may not be a real type. replace it
    with int if it does not work, prelude will surely
    give the correct answer
    Last edited by ggs; 07-12-2004 at 12:37 PM.
    .sect signature

  11. #11
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    thanks for the example.
    still not working thou , heres what i tried.

    Code:
    		fseek(fal, 0, SEEK_END);
    		pos = ftell(fal);
    		fseek(fal, 0, SEEK_SET);
    		fread(&ReadTxt , sizeof(ReadTxt) , 1 , fal);
    pos is int

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't work because you don't do anything with pos. You simply store it. You will want to use the end position to figure out the size you need to allocate. You're not doing this here. You're still simply reading whatever the size of ReadTxt is.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    damn this is confusing.sorry for my stupidness but i dont no how to do this

  14. #14
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    i dont quiet understand fread right is it

    fread(variable , eof , startof , file);

    ?

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    fread( tothisplace, thisisthesizeofanobjecttoread, thisishowmanyobjectstoread, fromthisfile );
    So find out how big the file is, place that as argument two. Place the number 1 as argument three. Make the first argument a pointer to some place where you've allocated enough memory to hold the total size you're passing (multiply argument two by argument three for total needed size. Add room for the null, assuming you're reading text).

    Quzah.
    Last edited by quzah; 07-12-2004 at 05:27 PM.
    Hope is the first step on the road to disappointment.

Page 1 of 3 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