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

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
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

Reply

Join Date: Dec 2008
Posts: 3
Reputation: dharma30 is an unknown quantity at this point 
Solved Threads: 0
dharma30 dharma30 is offline Offline
Newbie Poster

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

 
0
  #1
Dec 10th, 2008
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!.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: dharma30 is an unknown quantity at this point 
Solved Threads: 0
dharma30 dharma30 is offline Offline
Newbie Poster

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

 
0
  #2
Dec 10th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 23
Reputation: low_coder is an unknown quantity at this point 
Solved Threads: 2
low_coder low_coder is offline Offline
Newbie Poster

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

 
0
  #3
Dec 10th, 2008
; 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: dharma30 is an unknown quantity at this point 
Solved Threads: 0
dharma30 dharma30 is offline Offline
Newbie Poster

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

 
0
  #4
Dec 10th, 2008
Originally Posted by low_coder View Post
; 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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #5
Dec 10th, 2008
>>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?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

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

 
0
  #6
Dec 10th, 2008
Originally Posted by Ancient Dragon View Post
>>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.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #7
Dec 10th, 2008
Originally Posted by Luckychap View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

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

 
0
  #8
Dec 10th, 2008
Originally Posted by Ancient Dragon View Post
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
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
1
  #9
Dec 11th, 2008
ASCII stupid question, get a stupid ANSI
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 23
Reputation: low_coder is an unknown quantity at this point 
Solved Threads: 2
low_coder low_coder is offline Offline
Newbie Poster

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

 
0
  #10
Dec 12th, 2008
; 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, 1 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC