C program to read n files and append it to a variable.


 
Thread Tools Search this Thread
Top Forums Programming C program to read n files and append it to a variable.
# 1  
Old 06-12-2012
Error C program to read n files and append it to a variable.

I am struck in the code of handling multiple files reading and appending it into a variable.

My First file's entire content I have concatenated using strcat(<char pointer>,char array)

In the Second file I extract using strtstr() function and below is the code.

Code:
if( fp2 != NULL){
            while(fgets(a, sizeof a, fp2) != NULL){
               if(strstr(a,"-----BEGIN") != NULL){
                 printf("%s",a);  // For selecting this line too...
                 while((i=fgetc(fp2)) != EOF){
                   printf("%c",i);
               //      strcat(con,i);
                 }
               }
            }
          }

I get error Like Segmentation problem while processing the line strcat in the code above.

And after this in the third file, if I use the strcat() to append the contents it's too not working....

I declared the variables like,

Code:
FILE *fp2;
char a[100],*con,i;
con =(char*)malloc(1000);

Please help ! Smilie


Moderator's Comments:
Mod Comment Please use code tags, thanks!

Last edited by zaxxon; 06-12-2012 at 09:59 AM.. Reason: code tags, see PM
# 2  
Old 06-12-2012
You cannot strcat a character. You strcat entire strings. That will cause a segmentation fault.

So use fgets instead of fgetc there.

Also: What code gets run here depends on the content of the files. You have not shown any content from your files. Please do so.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-13-2012
MySQL

ya. That's true ! Thanks for explaining me correctly !Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read files into variable using then pass to delete call

I am trying read all the files from list into a variable line using bash. After there are read into the variable they are passed to a delete call. The files appear to be read line (as I can see them with the echo) by line into the variable, but the delete call is not removing them and I do not... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Read line from the file and append it to each row

Hi All, We have a file in the following format: 0.010000 $ ITI 11 LV2 $ 40456211 $ 0.135000 $ ITI 11 LV1 $ 40512211 $ 1.215600 $ ITI 11 ITI3 $ 41406211 $ 24/05/2014 14:05:02 0.030000 $ ITI 11 LV2 $ 40456211 $ ... (3 Replies)
Discussion started by: gauravsinghal79
3 Replies

3. Shell Programming and Scripting

Read files in shell script code and run a C program on those files

HI, I am trying to implement a simple shell script program that does not make use of ls or find commands as they are quite expensive on very large sets of files. So, I am trying to generate the file list myself. What I am trying to do is this: 1. Generate a file name using shell script, for... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

4. Shell Programming and Scripting

Read multiple files, parse data and append to a file

Hi..Can anyone suggest a simple way of achieving this. I have several files which ends with extension .vcf . I will give example with two files In the below files, we are interested in File 1: 38 107 C 3 T 6 C/T 38 241 C 4 T 5 C/T 38 247 T 4 C 5 T/C 38 259 T 3 C 6 T/C... (8 Replies)
Discussion started by: empyrean
8 Replies

5. Shell Programming and Scripting

How can I read variable text files through cat command?

Hi. I'm just wondering how can I read variable text files through cat command. I made a shell script to count frequency of words and array them with variable conditions. It's only working for one file that I wrote in script now. But I want to make it working for every text file when I execute... (2 Replies)
Discussion started by: rlaxodus
2 Replies

6. Shell Programming and Scripting

Append variable texts to the beginning of each line in all files in a directory

I am writing a code to append some numbers in the beginning of each line in all the files present in a directory. The number of files are really huge. The files are numbered as 1.sco, 2.sco, 4.sco (Note: 3.sco is missing). The files currently look like this: 1.sco 2 3 5 6 6 7My task is to... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

7. Shell Programming and Scripting

How to read a file line by line and store it in a variable to execute a program ?

Hello, I am quite new in shell scripting and I would like to write a little scritp to run a program on some parameters files. all my parameters files are in the same directory, so pick them up with ls *.para >>dirafter that I have a dir file like that: param1.para param2.para etc... I... (2 Replies)
Discussion started by: shadok
2 Replies

8. Shell Programming and Scripting

How to read and append certain files form directory

Hi ,i have a question ,if I am in sertain directory and I have 4 files *.c how can I read and append this files in file backup.bac Thanks find ./ -name "*.csh" | wc -l (2 Replies)
Discussion started by: lio123
2 Replies

9. Shell Programming and Scripting

read from a specific pattern from one file and append it to another

Hi! Everyone, Say this file1 -------------- line 1 51610183 420001010 0010CTCTLEDPPOO 2151610183 line 2 2151610183 420001010 0030A2TH2 line 3 2151610183 420001010 0040A2TH3 line 4 2151610183 420001010 ... (0 Replies)
Discussion started by: kinkar_ghosh
0 Replies

10. Shell Programming and Scripting

Perl program to read from multiple files

Hi, I need to generate a test data file by reading inputs from multiple files. A sample pseudo code for this program to read from three files and write to a output file would be like: open(OUTPUTFILE, "data"); open(INFILE1, "data1"); open(INFILE2, "data2"); open(INFILE3, "data3"); ... (1 Reply)
Discussion started by: jyotipg
1 Replies
Login or Register to Ask a Question