File content handling in c


 
Thread Tools Search this Thread
Top Forums Programming File content handling in c
# 1  
Old 02-21-2012
Error File content handling in c

HI,

I have a file with the following content like
--------filename-----------
this is a tool
end
--------filename-----------
this is a tool
this is a box
end
--------filename-----------
these are tools
these are boxes
this is one
end

i want to write the content to another file as follow's

--------filename-----------
this is a tool
--------filename-----------
this is a tool
--------filename-----------
this is a box
--------filename-----------
these are tools
--------filename-----------
these are boxes
--------filename-----------
this is one

where filename is variable.

Please help me with the solution in c language.
# 2  
Old 02-21-2012
What have you tried?
# 3  
Old 02-22-2012
HI,

I am using this for coping the data from one file to other file with file name between each file.

Code:
while(!feof(stdin)) {
        while ((fgets(strLine,sizeof strLine, stdin) != NULL) && (!feof(stdin))) {

        if ((strcmp(strLine, "File") != 0) && (strcmp(strLine, "Total=") != 0)) {


                                fprintf (fp1, strLine);
        }
        }
        fclose(fp1);
        fp1 = fopen("FinalScan.temp", "r");

        while ((fgets(strLine,sizeof strLine, fp1) != NULL) && (!feof(fp1))) {

        if ((strcmp(strLine, "File") != 0)) {

                                strcpy(strLine2,strLine);
                                fprintf (fp, strLine);
        }
        }

Regards,
Jhon.
# 4  
Old 02-22-2012
Any particular reason you want to do this in C? It'd be a snap in shell...

Some of that's a bit redundant.
Code:
char buf[1024], header[1024];

header[0]='\0';

while(fgets(buf, sizeof(buf), stdin))
{
        if(strncmp(buf, "----", 4) == 0)
        {
                strcpy(header, buf);
                continue;
        }

        fputs(header, stdout);
        fputs(buf, stdout);
}

Should match the ---- lines, and print them first for every line that isn't one.
# 5  
Old 02-22-2012
How we'll redirect this to a file???
# 6  
Old 02-22-2012
Code:
./program < input > output

Or, if that's not what you want, please explain what you do. Your original explanation showed it all being read from one big file and all being written to one big file.
# 7  
Old 02-23-2012
Thank you for help got the solutions..Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File handling

Hi All, I need to extract the data from the text file. The data of the text file is shown below #L 0.000017 4.329939 0.000017 4.716267 r7.9 P 1 1;Net=IN32 The extracted data should be IN32. Could anyone help to script in c shell.? (4 Replies)
Discussion started by: gopishrine
4 Replies

2. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

3. UNIX for Dummies Questions & Answers

File handling

I have a file 1 298167 298168 1093209 1093210 1422663 I want to write a code where in I want to read contents of above file like first read is 1 second read is 298167 Substract second read from first and if this is greater than or less than 99,999. Similarly I want to traverse thru... (3 Replies)
Discussion started by: Guru148
3 Replies

4. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

5. UNIX for Dummies Questions & Answers

File Handling

Hi Team, I am trying to cut a large file into multiple files. It has Header 50,050 records Trailer ------------------------------------------- I need to cut the files into multiple files of 1000 records and should have the same header and trailer as the original files. ... (4 Replies)
Discussion started by: Gurkamal83
4 Replies

6. UNIX for Advanced & Expert Users

File handling

my input for a script is another csv file but in that file say 7 lines are there ... how can i get line by line to that input for example : >cat link.csv www.yahoo.com,yahoo www.google.com,google www.unix.com,unix another file in that file i need to ping the above links ... (2 Replies)
Discussion started by: ponmuthu
2 Replies

7. UNIX for Advanced & Expert Users

please help me in file handling

sir i have to get first line from a file for example >cat file1 abc zxc asd adsf from that file1 i need only first line expected result >abc please help me ! (1 Reply)
Discussion started by: ponmuthu
1 Replies

8. UNIX for Advanced & Expert Users

File Handling

Hi, I have a log file which runs into 3 to 5 GB. We store this typically for 6 months. When a new month starts we move the previous month into a 9 month back up log (file.9m) and delete the last month of the 9 month back up. Iam using awk to find the data and cat to join the files like... (3 Replies)
Discussion started by: baanprog
3 Replies

9. Programming

file handling

Hi all, I got a little issue here. Imagine that I have more than one process accessing one file. Is it possible to know which process(es) are accessing that file when I open the file?? Thanks for the help. Best regards, Ernesto (2 Replies)
Discussion started by: ninjanesto
2 Replies

10. Programming

File Handling in C

Hi all, I have a problem in handling files through C. here is the problem im having: i will query the database (for instance consider employees table ) for empno,ename,job,salary fields.The query returns me some 100 of rows. now i need to place them in a file in row wise pattern as they... (3 Replies)
Discussion started by: trinath
3 Replies
Login or Register to Ask a Question