![]() |
|
#11
|
|||
|
|||
|
My intension was not to force you to use linked list. If you haven't yet learned it in your class, in this program you can use static storage such as arrays. Infact that would be a good way to start.
Again in your new post there are several mistakes. in fscanf you are collecting strings in single characters CPP / C++ / C Code:
all the variables you are supplying above are defined as char which will hold only 1 byte and can not hold strings. also, you do not need & operater when you are collecting string using an array of chars. Following statement is valid provided you change all your variables to either char array or char pointers. CPP / C++ / C Code:
Secondly,to answer your question, Yes, you can define a structure inside a function. However, beware that its scope is limited to that function. If you wanna define a new storage type, its better you do it globally so that any function in the source file can use the storage definition to define new variables of that type. There is one problem here in your structure definition inside main() I am just correcting it, leaving the understanding part to you. CPP / C++ / C Code:
Finally, not trying to discourage you but, I would suggest you not to directly jump into pointers and linked list. Get this program work with simple static storage for everything. This will also help you in eliminating all syntax related errors in your code. If you want to learn about linked list, there are some tutorials in this forum on that. You can also use following link. http://juicystudio.com/tutorial/c/linked.asp Good Luck, |
|||
|
#12
|
|||
|
|||
|
thanks nkhambal, I'll read the tutorial.
I thought using linked lists would be easier, but I'll go with static storage like you said since I do nothave all the knowledge yet. Now I can read each line of my file and separate groups of 6 line to create struct, but somehow my buffer is confused and I get only half-word :-? I used a switch to manage the linformation I get from the file depending on it's line_number. This way a line #1 is aadded to the record #1 of a struct. I also think I have a problem with my array books which is supposed to contain every struck (book or group of 6 lines) because when I use books(value) it doesn't return me what it is supposed to... Thanks for all your help guys, I wish I can finish this by tonight CPP / C++ / C Code:
|
|
#13
|
|||
|
|||
|
Can you show us how you have arranged data in your text file. ? Post one entry for full record(all members of structure) in the way it is stored in file.
Also, I still see you mixing dynamic storage with static. I would recommend stick to static for now. Define static variables instead of char * and avoid mallocs and reallocs. Its unncessarily complicating a simple things. Onces it works fine with static storage, you can think of migrating or optimizing it with dynamic storages using pointers and linked list. |
|
#14
|
|||
|
|||
|
This is an example of what should give 2 struct
The Big Bang GRIBBIN, JOHN FLAMMARION 208081270X Science 10/6/1992 The Bits RUSSELL BERTRAND UNION GEN. D'ED. 2264025751 Computers 4/3/1997 keep in mind that they are always in this order Title Author Editor ISBN (code) Subject Release date Hope its help. Frank |
|
#15
|
||||
|
||||
|
Quote:
The statement CPP / C++ / C Code:
Code:
This segment of code should cause you no end of headaches: CPP / C++ / C Code:
1) make books bigger and assign the address to temp 2) reassign the pointer in tempback to books 3) then allocate yet another entry in books Instead of all these memory gyrations, simply define the books array to handle twice the data in your file and get rid of all memory allocations. You're getting too complicated for a beginner and don't yet have the techniques down for this higher level concepts. And you don't need temp at all. And: CPP / C++ / C Code:
1) a function to move the entries from one to the other 2) use memcpy() to move all the bytes from one to the other Then CPP / C++ / C Code:
1) read line 1 in the while statement 2) process the line 3) read line 2 at the bottom of the while 4) read line 3 in the while statement so you are only processing every other line from the file. This should keep you busy, along with what nkhambal said above. Also, you know that when you say: CPP / C++ / C Code:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#16
|
|||
|
|||
|
Hi kingpin,
I hate to do this but since i see you struggling with this code, here is the code that I worked on. Its with static storage (array). This works fine. This will give you some idea and baseline to work on if you wan to work on it with linked list. CPP / C++ / C Code:
Good Luck. |
|
#17
|
||||
|
||||
|
Quote:
As I've said elsewhere, never Never NEVER NEVER use feof() to test for the end of file to terminate a loop. It will usually fail because it will not return TRUE until after (not when but AFTER) the end of file is reached -- which means you will get one bogus record. And instead of reading a character at a time, read an entire line with fgets(). Most of your getline() routine can be changed to CPP / C++ / C Code:
Also, you did not specify a type for getline(). By default you've defined it as an int so you need to return an int to the calling function. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 11:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 12:28 |
| Re: Programming Techniques | WaltP | C Programming Language | 0 | 10-Mar-2004 00:56 |
| reading and writing to a file in C++ | mgp6q | C++ Forum | 17 | 02-Mar-2004 13:42 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The