943,865 Members | Top Members by Rank

View Poll Results: Is there a solution to the above question?
Yes 1 100.00%
No 0 0%
Cant Say 0 0%
Voters: 1. You may not vote on this poll

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 922
  • C RSS
Dec 10th, 2008
0

How to read a table from a text file and store in structure.

Expand Post »
Hello Everyone!,

This Forum has helped me very much in understanding different solutions in C-language . Before I post my query i would like to thank everyone for there help in the past.


Q:
I have a text file say "input.txt" which contains a table in the following form

NAME LVL OCCURS TYPE SCOPE LENGTH DEC BYTES
0YDFSE 01 CHA GLOBAL 172 172
1DERSW 05 5 NUM LOCAL 72 4 100

Now, my question is can i read the contents of the table into a structure and will i be able to print these contents in desired form in another file say "output.txt"

LVL NAME OCCURS DEC
01 0YDFSE
05 1DERSW 5 4

Also, Can we read the spaces under the 1st row of occurs and store in a structure variable say struct[0].occurs.

I am really confused how to approach this.

Your Help would make me the happiest man! Please suggest!.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dharma30 is offline Offline
3 posts
since Dec 2008
Dec 10th, 2008
0

Re: How to read a table from a text file and store in structure.

The table in input file is .
  1. NAME LVL OCCURS TYPE SCOPE LENGTH DEC BYTES
  2. 0YDFS 01 ### CHA GLOBAL 172 ### 172
  3. 1DERS 05 5 NUM LOCAL 72 4 72
There was some whitespace problem in the before post.

###---> White spaces
Last edited by dharma30; Dec 10th, 2008 at 4:40 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dharma30 is offline Offline
3 posts
since Dec 2008
Dec 10th, 2008
0

Re: How to read a table from a text file and store in structure.

; yes you can separate data in the text or any other extension file ;with some characters (e.g: "<Name>Max</Name>" and so on) or ;you can separate data with e.g.: 0A4h or some other non-letter or ;number character.
; then your program must search for occurrence of those characters ;and fill the structure.
Reputation Points: 40
Solved Threads: 4
Junior Poster in Training
low_coder is offline Offline
55 posts
since Nov 2008
Dec 10th, 2008
0

Re: How to read a table from a text file and store in structure.

Click to Expand / Collapse  Quote originally posted by low_coder ...
; yes you can separate data in the text or any other extension file ;with some characters (e.g: "<Name>Max</Name>" and so on) or ;you can separate data with e.g.: 0A4h or some other non-letter or ;number character.
; then your program must search for occurrence of those characters ;and fill the structure.

Sorry, I did not understand what you meant here. Can u give a small example?.Perhaps I framed the question wrong. I want to use C-language to code for this question. Is that possible?.

Any suggestions how to work on this with C-Language?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dharma30 is offline Offline
3 posts
since Dec 2008
Dec 10th, 2008
0

Re: How to read a table from a text file and store in structure.

>>can i read the contents of the table into a structure
I don't know -- can you ?

>>will i be able to print these contents in desired form
Here again, I don't know if you can or not. Only you can tell us that. And why should we care if you can or not?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Dec 10th, 2008
0

Re: How to read a table from a text file and store in structure.

>>can i read the contents of the table into a structure
I don't know -- can you ?

>>will i be able to print these contents in desired form
Here again, I don't know if you can or not. Only you can tell us that. And why should we care if you can or not?
HAHAHA, Dragon is angry today.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Dec 10th, 2008
0

Re: How to read a table from a text file and store in structure.

Click to Expand / Collapse  Quote originally posted by Luckychap ...
HAHAHA, Dragon is angry today.
Nope, not angry, just answering his questions. He has to learn to ask the right question(s) if he wants better answers.

That like someone asks me "Do you know what time it is?". I look at my watch and answer "Yes", then continue with what I was doing.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Dec 10th, 2008
0

Re: How to read a table from a text file and store in structure.

That like someone asks me "Do you know what time it is?". I look at my watch and answer "Yes", then continue with what I was doing.

Man you are just in mood
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Dec 11th, 2008
1

Re: How to read a table from a text file and store in structure.

ASCII stupid question, get a stupid ANSI
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 12th, 2008
0

Re: How to read a table from a text file and store in structure.

; I have written an example for you.
; try this:
; all files are in attachement

  1. #include <windows.h>
  2. #include <winioctl.h>
  3.  
  4. struct Record
  5. {
  6. char Name[20];
  7. char LVL[20];
  8. char OCCURS[20];
  9. char TYPE[20];
  10. char SCOPE[20];
  11. char LENGTH[20];
  12. char DEC[20];
  13. char BYTES[20];
  14. };
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18. HANDLE hFile;
  19. DWORD file_size, hMemory, dwRead;
  20. LPTSTR pMemory;
  21. struct Record cur_data;
  22. int i, cur_record, cur_field, n;
  23. hFile = CreateFile("1.txt", GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  24. file_size = GetFileSize(hFile, 0);
  25. hMemory = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, file_size);
  26. pMemory = GlobalLock(hMemory);
  27. ReadFile(hFile, pMemory, file_size, &dwRead, NULL);
  28. cur_field = 0;
  29. cur_record = 0;
  30. n = 0;
  31. for (i = 0; i < file_size; i++)
  32. {
  33. if (pMemory[i] == 0x01)
  34. {
  35. switch (cur_field)
  36. {
  37. case 0:
  38. cur_data.Name[n] = 0x0;
  39. case 1:
  40. cur_data.LVL[n] = 0x0;
  41. case 2:
  42. cur_data.OCCURS[n] = 0x0;
  43. case 3:
  44. cur_data.TYPE[n] = 0x0;
  45. case 4:
  46. cur_data.SCOPE[n] = 0x0;
  47. case 5:
  48. cur_data.LENGTH[n] = 0x0;
  49. case 6:
  50. cur_data.DEC[n] = 0x0;
  51. case 7:
  52. cur_data.BYTES[n] = 0x0;
  53. }
  54. n = 0;
  55.  
  56. if (cur_field == 7)
  57. {
  58. cur_field = 0;
  59. cur_record++;
  60. printf("Current Record: %d\n",cur_record);
  61. printf("Name: %s\n",cur_data.Name);
  62. printf("LVL: %s\n",cur_data.LVL);
  63. printf("OCCURS: %s\n",cur_data.OCCURS);
  64. printf("TYPE: %s\n",cur_data.TYPE);
  65. printf("SCOPE: %s\n",cur_data.SCOPE);
  66. printf("LENGTH: %s\n",cur_data.LENGTH);
  67. printf("DEC: %s\n",cur_data.DEC);
  68. printf("BYTES: %s\n\n",cur_data.BYTES);
  69. }
  70. else
  71. {
  72. cur_field++;
  73. }
  74. }
  75. else
  76. {
  77. switch (cur_field)
  78. {
  79. case 0:
  80. cur_data.Name[n] = pMemory[i];
  81. case 1:
  82. cur_data.LVL[n] = pMemory[i];
  83. case 2:
  84. cur_data.OCCURS[n] = pMemory[i];
  85. case 3:
  86. cur_data.TYPE[n] = pMemory[i];
  87. case 4:
  88. cur_data.SCOPE[n] = pMemory[i];
  89. case 5:
  90. cur_data.LENGTH[n] = pMemory[i];
  91. case 6:
  92. cur_data.DEC[n] = pMemory[i];
  93. case 7:
  94. cur_data.BYTES[n] = pMemory[i];
  95. }
  96. n++;
  97. }
  98. }
  99. GlobalUnlock(pMemory);
  100. GlobalFree(hMemory);
  101. CloseHandle(hFile);
  102. return 0;
  103. }
Attached Files
File Type: zip Release.zip (12.2 KB, 15 views)
Reputation Points: 40
Solved Threads: 4
Junior Poster in Training
low_coder is offline Offline
55 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Do I still have to use While or for Loop???
Next Thread in C Forum Timeline: Read Hardware Information





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC