![]() |
|
#1
|
|||
|
|||
Problem reading structs in a fileHi all, I have a file containing some info on separated lines, each six lines represent a struct. I'm trying to read these structs and put them in an array of structs so I can and print them after. Just wanted to get some feedback on my function, I'm still new and want some help before continuing. Thanks for your time
Here's what it looks like. CPP / C++ / C Code:
Last edited by JdS : 25-Oct-2004 at 06:10.
Reason: Please insert your example C/C++ codes between [c] and [/c] tags
|
|
#2
|
||||
|
||||
|
Quote:
When posting, it helps to tell us what kind of feedback you want, many of us will have a field day ripping your code apart giving you no real help :-) So what type of feedback do you want? Does your code work? If not (and that's the feedback you want) you have to let us know what's wrong, where it's wrong, and what you think the problem is. And I know somewhere on the site (maybe in a sticky?) there's an axplanation how to post code so it's readable. Therefore, my first "feedback" is: Quote:
__________________
Age is unimportant -- except in cheese |
|
#3
|
|||
|
|||
|
Thanks Walt for not flamming me :-)
Right now the file is opened correctly and I'm able to read each line and print them in a new file. My problem is to insert each struc t(which iare made of 6 consecutive lines) as an item in my array. CPP / C++ / C Code:
When reading a line in the file books.txt, this is how I currently assign a value to a field of the struct infoBook. Then, when the struct is completed (with the 6 fields having value), I need to add this struct to the array books, which I'm not able right now :-? I'm not sure my structure is correct, but I tried regrouping all the info I learned so far... Here's the complete code CPP / C++ / C Code:
Last edited by dsmith : 24-Oct-2004 at 15:47.
Reason: Use [c] & [/c] for c syntax not [code]
|
|
#4
|
|||
|
|||
|
what is the definition of array "c". I don't see it declared anywhere in the code. Also, is "infoLivre" of same type as "bookInfo" ?. You are mallocing memory of size "infoLivre" and reallocing of size "bookInfo". I don't see a code line for reading the line from file. How you are getting values from file?
Am i missing here something? Can you pls post reply to these queries to better understand your code. Also, pls use code tags when posting your code. Thanks, |
|
#5
|
|||
|
|||
|
Quote:
Livres = Books, I translates all my code so I might have forgotten to change certain word. Also, the declaration is char c[50]; and not x, I used it to test and post the old one, sorry :-? Thanks for your help |
|
#6
|
|||
|
|||
|
you haven't yet explained how are you fetching data from the file. I guess you either forgot to code it or you forgot to paste it in your post.
whichever it is, you still need a code line to fetch data from file. I would suggest (if you have not already coded it) use fgets() and sscanf() conbination to read words from file. |
|
#7
|
||||
|
||||
|
Quote:
Quote:
Also, code tags are only useful for keeping your formatting intact so your post can be read. But if you have no formatting, they really are useless. You might want to read this tutorial for the hows and whys of formatting. You're writing long enough code that it's already important to get in the habit. By the way, aren't instructors going over formatting code these days? If not, could you ask yours "why not?" I'm very interested why this important step is being left out of the training. __________________
Age is unimportant -- except in cheese |
|
#8
|
|||
|
|||
|
Ok post the function the parse each line into a struct, which will be add to an array of struct once it is complete. I try adding some things I found on the web since I'm still new to C.
Currently I have 2 problem when compiling: -"BOOK book;" -"books[num_books] = book;" I thought we could declare a new instance of a struct in the function but it gives me the "Undeclared identifier"... The other error says "cannot convert from int to bookInfo", but I'm trying to assign a value from a struct book to the num_books value of the struct array, which is a struct... :-? Any help would be appreciate. Thanks Code:
|
|
#9
|
|||
|
|||
|
Here are few comments about your code. They are in no specific order. These are not exhaustive comments. Other might come up with more.
1. You can not use "buf" without allocating memory to it. Since buf is a un initialized pointer, it does not point to any allocated memory location. Your program will crash at fgets(). 2. Secondly, Do you mean same when you type BOOK and book? . They are not same. BOOK is different than book. There is no definition for BOOK in your code. Your compiler will flags error as unknown storage type. 3. Following lines CPP / C++ / C Code:
should be changed to CPP / C++ / C Code:
4. You should check of EOF and not NULL in following line of code. CPP / C++ / C Code:
CPP / C++ / C Code:
5. This line is wrong as there is no storage defined by name BOOK and book is already a defined structure. CPP / C++ / C Code:
6. If you want to setup up a dynamic storage of books. You need to define a linked list where each node (a book entry) will hold the address of next node. Books needs to added and removed to dynamic linked list. You are mixing stating and dynamic storage. You are not linking the books. They are scattered all over your memory and may not be in contiguous blocks of memory, cuase you are dynamiclly allocating the storage for book using malloc() and reallloc(). Pls read about linked lists to build this type of dynamic storages. For starters, I can give you a code to modify your structure book to make a linked list. In below code, pointer variable next of type struct book, points to the next memory location where the next book entry is stored. CPP / C++ / C Code:
so finally your book list should look like. CPP / C++ / C Code:
where book1 is first node in list and bookn is last node. My 2 cents. |
|
#10
|
|||
|
|||
|
Quote:
Ok I'll try using linked lists even if I haven't see them in class yet. If anyone could set me on the right track that would be very appreciated. BTW can we defined a new struct directly in a function ? See below CPP / C++ / C Code:
|
Recent GIDBlog
Python ebook 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 10:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 11:28 |
| Re: Programming Techniques | WaltP | C Programming Language | 0 | 09-Mar-2004 23:56 |
| reading and writing to a file in C++ | mgp6q | C++ Forum | 17 | 02-Mar-2004 12:42 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The