| | |
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 | |||
![]() |
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
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!.
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!.
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
The table in input file is .
There was some whitespace problem in the before post.
###---> White spaces
C Syntax (Toggle Plain Text)
NAME LVL OCCURS TYPE SCOPE LENGTH DEC BYTES 0YDFS 01 ### CHA GLOBAL 172 ### 172 1DERS 05 5 NUM LOCAL 72 4 72
###---> White spaces
Last edited by dharma30; Dec 10th, 2008 at 4:40 am.
•
•
Join Date: Nov 2008
Posts: 23
Reputation:
Solved Threads: 2
; 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.
; then your program must search for occurrence of those characters ;and fill the structure.
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
; 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?
>>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?
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?
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
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.
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.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
•
•
Join Date: Nov 2008
Posts: 23
Reputation:
Solved Threads: 2
; I have written an example for you.
; try this:
; all files are in attachement
; try this:
; all files are in attachement
C Syntax (Toggle Plain Text)
#include <windows.h> #include <winioctl.h> struct Record { char Name[20]; char LVL[20]; char OCCURS[20]; char TYPE[20]; char SCOPE[20]; char LENGTH[20]; char DEC[20]; char BYTES[20]; }; int main(int argc, char *argv[]) { HANDLE hFile; DWORD file_size, hMemory, dwRead; LPTSTR pMemory; struct Record cur_data; int i, cur_record, cur_field, n; hFile = CreateFile("1.txt", GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); file_size = GetFileSize(hFile, 0); hMemory = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, file_size); pMemory = GlobalLock(hMemory); ReadFile(hFile, pMemory, file_size, &dwRead, NULL); cur_field = 0; cur_record = 0; n = 0; for (i = 0; i < file_size; i++) { if (pMemory[i] == 0x01) { switch (cur_field) { case 0: cur_data.Name[n] = 0x0; case 1: cur_data.LVL[n] = 0x0; case 2: cur_data.OCCURS[n] = 0x0; case 3: cur_data.TYPE[n] = 0x0; case 4: cur_data.SCOPE[n] = 0x0; case 5: cur_data.LENGTH[n] = 0x0; case 6: cur_data.DEC[n] = 0x0; case 7: cur_data.BYTES[n] = 0x0; } n = 0; if (cur_field == 7) { cur_field = 0; cur_record++; printf("Current Record: %d\n",cur_record); printf("Name: %s\n",cur_data.Name); printf("LVL: %s\n",cur_data.LVL); printf("OCCURS: %s\n",cur_data.OCCURS); printf("TYPE: %s\n",cur_data.TYPE); printf("SCOPE: %s\n",cur_data.SCOPE); printf("LENGTH: %s\n",cur_data.LENGTH); printf("DEC: %s\n",cur_data.DEC); printf("BYTES: %s\n\n",cur_data.BYTES); } else { cur_field++; } } else { switch (cur_field) { case 0: cur_data.Name[n] = pMemory[i]; case 1: cur_data.LVL[n] = pMemory[i]; case 2: cur_data.OCCURS[n] = pMemory[i]; case 3: cur_data.TYPE[n] = pMemory[i]; case 4: cur_data.SCOPE[n] = pMemory[i]; case 5: cur_data.LENGTH[n] = pMemory[i]; case 6: cur_data.DEC[n] = pMemory[i]; case 7: cur_data.BYTES[n] = pMemory[i]; } n++; } } GlobalUnlock(pMemory); GlobalFree(hMemory); CloseHandle(hFile); return 0; }
![]() |
Similar Threads
- Database manipulation with AVL tree.. please help (Java)
- Variable scope problem (C++)
- dynamically update pages (HTML and CSS)
Other Threads in the C Forum
- Previous Thread: Do I still have to use While or for Loop???
- Next Thread: Read Hardware Information
Views: 701 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue mysql number oddnumber odf opensource overwrite owf pdf performance pointer pointers posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






