C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 25th, 2014, 11:11 AM
sniperwolf500 sniperwolf500 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2014
Posts: 1 sniperwolf500 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 15 sec
Reputation Power: 0
File IO - Read from file into array of struct

I have a struct of students that written to a file all what i want is intial the array with values from the file using any method , the aim is to put the struct values into array to help sorting process the code gives me punch an kick any idea how to achive it


1. #include <stdio.h>
2. #include <string.h>
3. #include <conio.h>
4. #include <stdlib.h>
5. #include <windows.h>
6. struct student
7. {
8. char id[5];
9. char name[30];
10. int term;
11. float gpa;
12. char grade;
13. };
14.
15. struct student stu;
16.
17. typedef struct student stud;
18.
19.
20. int main ()
21. {
22. FILE *fp;
23. fp = fopen("record.txt","ab+");
24. char nextChar = getc(fp);
25. int numCharacters = 0;
26.
27. while (nextChar != EOF)
28. {
29. numCharacters++;
30. nextChar = getc(fp);
31. }
32. //////// detect number of characters ////////
33.
34. int chunck = numCharacters/sizeof(stu);
35.
36. stud *arr = (stud *)malloc(chunck);
37.
38. /*//problem comes next ...
39. // intial the array of students with records from file
40. while(fread(&stu,sizeof(stu),1,fp) == 1)
41. {
42. for(d=0;d<chunck;d++)
43. {
44. arr[d].id=stu.id;
45. arr[d].name=stu.name;
46. arr[d].term=stu.term;
47. arr[d].gpa=stu.gpa;
48. }
49. }*/
50.
51. return 0;
52. }

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > File IO - Read from file into array of struct


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap