![]() |
|
Files in C, fseek |
Paul whoknows
Member #5,081
September 2004
![]() |
There is 2 files: EMPLOYEES.DAT and STUDENTS.DAT(*** DOWNLOAD ATTACHMENT ***) EMPLOYEES.DAT (3 records) SURNAME NAME SALARY Hunter Rick 1000 Tokisada Amakusa S. 1000 Duncan Seymour 1200 --------------------------------------- STUDENTS.DAT (2 records) SURNAME NAME AVERAGE Hunter Rick 8 Tokisada Amakusa S. 9
update_salary function must allow user to update salary field of those employees who are also students and average >7, in other words: Therefore, update_salary should show these records: Hunter Rick 8 Tokisada Amakusa S. 9
But it doesn't, it only shows one of them.
____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
Physics Dave
Member #4,427
March 2004
|
If I remember, fwrite moves the position in the file along by however much it writes, so that second fseek is not needed: I'd also ask you to watch your brackets and indentations: { it will make stuff { a lot clearer } honest! } h2g2bob |
A J
Member #3,025
December 2002
![]() |
looks like homework! which bit of the fseek documentation dont you understand ? ___________________________ |
BAF
Member #2,981
December 2002
![]() |
Why are you writing structs directly to the file? |
Paul whoknows
Member #5,081
September 2004
![]() |
Quote: If I remember, fwrite moves the position in the file along by however much it writes, so that second fseek is not needed: I did what you said, and it almost works, but it really doesn't, because it get trapped in a neverending loop. Quote: which bit of the fseek documentation dont you understand ? I don't know how to control file pointers, I mean, fwrite and fread automatic moves the file pointer forward, but fseek doesn't work sometimes, look at my source, it must show 2 aforementioned records, but it doesn't. Quote: Why are you writing structs directly to the file?
Because I can add unlimited records to my files, that wouldn't be possible using static arrays, and we are not allowed to use dynamic memory allocation yet. ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
Evert
Member #794
November 2000
![]() |
Quote: Any advice?
Never, ever use fread and fwrite. Quote: we are not allowed to use dynamic memory allocation yet. Were you told not to, or haven't they told you how to do it? If the former, that sucks. If the latter, just use it if you think it's best. |
Paul whoknows
Member #5,081
September 2004
![]() |
Quote: Never, ever use fread and fwrite. What can I use instead? Quote: For advise, I'd tell you to store data on disk as a text file. Only binary files must be used, that's an order, that limitation is part of the problem. Quote: Were you told not to...
Yes, another order. ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
BAF
Member #2,981
December 2002
![]() |
Wow.. what kind of programming class is this if they limit what you can do and teach you to fread/fwrite structs. |
Paul whoknows
Member #5,081
September 2004
![]() |
Dynamic memory allocation, LIFO, FIFO, binary trees, all that stuff is comming later, but now they force us to use these "basic tools", don't ask me why, I am just a student. ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
A J
Member #3,025
December 2002
![]() |
for any text file you plan to parse, if its less than 100k, just read it into memory and use regular char* on it, it will be 1. faster. infact, i would go as far as 200k or 300k before reconsidering what i just said. ___________________________ |
GullRaDriel
Member #3,861
September 2003
![]() |
Or use some PACKFILE routines, with compressed-or-not PACKFILE *file. All of them are endian-safe. little example: PACKFILE *in; int value = 10; . . in = pack_fopen( "file.dat" , "rp" ); . pack_iputl( in , value ); /* write a number */ . value = pack_igetl( in ); /* read a number */ . pack_fclose( in );
"Code is like shit - it only smells if it is not yours" |
Paul whoknows
Member #5,081
September 2004
![]() |
Quote: for any text file you plan to parse, if its less than 100k, just read it into memory and use regular char* on it, it will be Only binary file must be used. Quote: Or use some PACKFILE routines Only ANSI C must be used.(I must remove conio.h) I appreciate all your help, and I am going to use it in my own projects, but I can't use it here, because of the aforementioned restrictions, thanks. ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
Bob
Free Market Evangelist
September 2000
![]() |
You can just read/write your data in some (opaque, binary) file instead of dynamically allocating memory for it. The file size will automagically grow as you write things into it, so you even have an automatically resizing container. It'll be ridiculously slow for any real-world application, but it may work for your case. Also, you may want to use fgets() to read the file one line at a time (although you'll need to deal with the really long line problem). Or, if you get to use C++, then std::readline(std::istream&, std::string&) will do the trick. -- |
Paul whoknows
Member #5,081
September 2004
![]() |
Quote: You can just read/write your data in some (opaque, binary) file instead of dynamically allocating memory for it. That's exactly what I am doing right now. Quote: Also, you may want to use fgets() to read the file one line at a time Can I use fgets in binary files? Quote: Or, if you get to use C++. . . ANSI C I must only use. ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
|