[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Hey guys last time I bother you with my poor grammar. So I need
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /adv/ - Advice

Thread replies: 20
Thread images: 5
File: 1.png (18 KB, 695x366) Image search: [Google]
1.png
18 KB, 695x366
Hey guys last time I bother you with my poor grammar.
So I need some help in creating a function that will read data from a txt file into an array of structures.
I am having trouble trying to store the data in the array list.
The exact requirements are as follows:
>>The function, loadNames, should be called to read the entire names.txt file and store the data from each line into a struct of type Name. The main function should pass an array of Name structs to loadNames so that it can simply read a line's data into the struct at the first element of this array, then read the second line's data into the second element, etc. This should be done for all 4429 lines of the names.txt file. Once loadNames has completed and returned to main, the file, names.txt, must never be read again during this execution of the application.
>>
File: 2.png (16 KB, 926x217) Image search: [Google]
2.png
16 KB, 926x217
Also also as I understand I can not use the string class when declaring the structure.
>>You should notice that the Name struct uses a C-String to store the name found on a line of the text file. You must use this struct and the c-string field, name, exactly as defined above. Using a c-strings requires library functions like, strcpy, strcmp, etc. (see textbook). If you want to use other strings in the application, you are welcome to use the data type, string, for these -- but not for the Name stuct's name field. The string type is found in many examples in the textbook.
>>
Dude, this isn't stackoverflow
>>
>>17368171
I know but this board has been a little more useful on the past :c
>>
File: text.png (34 KB, 431x676) Image search: [Google]
text.png
34 KB, 431x676
This is how the txt file look like
>>
>>17368164
Why not ask >>>/g/? I bet you most people here don't code on a regular basis, if at all.
>>
>>17368230
They usually get mad if threads like this are made, also it's against the rules
>>
>>17368171
people ask about construction projects here too
>>
>>17368241
Oh shit whoops. I don't frequent /g/ and haven't coded in years (and even then it was just for a quarter in college).

All i remember for our assignment was that it was really easy and just used a loop for all the data. You've got the loop already.

Shit sorry man.
>>
use c file io stuff

fopen the file
fgetc each character in the file and look for newlines and count them
that's the size of your array
now getline each line from your file
sprintf the first chunk to the char pointer in your name struct, again you can use fgetc to get the index of the first space
do the same for the rest of the stuff in the line

since your name struct has that int array

and of course you're probably not using an array of pointers to that struct so you don't have to worry about malloc/free
>>
File: 0w0.jpg (168 KB, 1024x1024) Image search: [Google]
0w0.jpg
168 KB, 1024x1024
>>17368257
Don't worry man and thanks at least I know I haven't been wasting my time
>>
>>17368271
Alright I will try this, thank you very much!
>>
>>17368271
also honestly this is more of a c type question than a c++ one

if it was c++ you could use STL or whatever the fuck you do with C++ turbo 3000 or whatever it is now... ofstream and all that shyte
>>
>>17368273
one more thing I thought of
if it's a known thing that all the names will be less than 21 chars then you are ok
otherwise you'd have to use that index o the first space in the line to tell what length of string you want to malloc
>>
>>17368282
and also write some unit tests
>>
>>17368273
>>17368276
>>17368284
Alright, thank you so much for the advice man!
>>
>>17368164
>>17368170
>>17368179
>>17368241
>>17368273
>>17368319

Regular /g/entooman here.

You SHOULD start threads like this on /g/. If a couple of autists get mad at you then fucking ignore them. Let them get their fucking panties in a twist. There will usually be a normal person who can help you.

Alternatively you can ask in the stupid questions thread, or the daily programming thread, but you're competing for attention with many other posts in those threads, so sometimes it's best to just make your own thread
>>
>/adv/
>this board isn't for advice
>>
File: whyyyy.png (17 KB, 741x356) Image search: [Google]
whyyyy.png
17 KB, 741x356
>>17369015
alright I will try thanks anon!
also update on the function. Right now the problem I have is that the function is not separating the data into names and number (int) pic related is what I have been trying now
>>
>>17369462
>/adv/
>this board isn't for solving your homework
Thread replies: 20
Thread images: 5

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.