Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need urgent help on converting a file into another format.

Status
Not open for further replies.

ab1966

Programmer
Jul 3, 2001
6
0
0
US
Hi C Gurus, Its been long time since I programmed in C. I almost forgot everything. Please help me writing the code. Here is what I need. I have a file with the following format :

Title Col1 Col2 Col3 Col4
Row1 X X
Row2 X X X
Row3 X X X

This is a comma delimited file. Now I want to convert it into another format as follows :
Col1, Row2
Col1, Row3
Col2, Row1
Col2, Row3
Col3, Row2
Col4, Row1
Col4, Row2
COl4, Row3

Please help me. Thanks in advance.

Ab
 
1st and foremost, is "row1" a row i.e., a complete line or is it the value of the column "title" ? Assuming it is the latter - thats what u'r description looks like to me, following is the solution :

u'll need an array of structures, since the referencing of variables seems to be quite random.

declare a structure say
str1
{
char title[T_SZ];
char col1[COL1_SZ];
char col2[..];
char col3[..];
char col4[..];
}

then declare an array of this structure,
str1 lines[NO_OF_LINES];

open the file, read line by line,
while in a line, get the column elements using the strncpy command. after reading of file is over, open your output file in write mode, and write in u'r required order.

here goes :

index = 0;
while(!feof(input_file_name))
{
memset(lines[index].title,NULL,sizeof(T_SZ));
memset(lines[index].row1,NULL,sizeof(T_SZ));
...
do a memset on all the elements of the structure...

// read line from file
fgets( line_of_file, 89, input_file_name );

strncpy(lines[index].title, line_of_file + position_of_column - 1, no. of chars in column);
//similarly do a strncpy as above and put all the col.s in the respective elements of the structure

index++;
}
fclose(input_file_name);

now that u have read the file, u can write it to u'r o/p file in any random order that u feel like, using fwrite, fprintf, etc..

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top