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!.

The table in input file is .

NAME       LVL    OCCURS   TYPE   SCOPE    LENGTH   DEC   BYTES
0YDFS        01    ###      CHA    GLOBAL    172    ###      172
1DERS       05     5        NUM    LOCAL      72     4       72

There was some whitespace problem in the before post.

###---> White spaces

; 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.

; 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?

>>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.

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.

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

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

#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;
}
This article has been dead for over six months. Start a new discussion instead.