Help in extracting data with command from file in C


 
Thread Tools Search this Thread
Top Forums Programming Help in extracting data with command from file in C
# 1  
Old 03-20-2008
Help in extracting data with command from file in C

Hi,

I have a file which stores the following array :-

1,2,3,4,5.........16,17,18,19,20

This file has few hundreds of inputs of these lines.

I would like to read this file one line at a time; and assign to an array which is separated by ",".

I tried to do fgets command however, segmentation fault occurs.I suspect its because I get the one whole line without concatenate the , .

Please help.

Thanks.
# 2  
Old 03-20-2008
A segfault means you have some other code problem. Please post your code.

Also fscanf() will allow you to specify field separators, so you can read one element of the array at a time.
# 3  
Old 03-20-2008
Hi,

The main motivation is that I would like to do a upper shell command on top of the binaries with different number of input files.

e.g ./binary.exe a.txt
./binary.exe b.txt

....where these a.txt and b.txt are input files.

The problem now i am facing is particular only on one input file before even I could move to perform some shell stuffs.

Below are some code snippet which I face error of seg fault i described earlier on.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 20

main (int argc, char *argv[])
{

FILE *cfPtr; /* for reading data to expand.txt for vector based */
FILE *tfPtr; /* for reading data from svmkb.txt */

int t[SIZE];
int subs[7];
int label=0;
int i;
char filename[5000];
char dump;

strcpy(filename,argv[2]);

tfPtr = fopen(filename,"r");
if (tfPtr == NULL)
{
printf("Can't find input file %s",filename);
exit(-1);
}


/*initialization for statement 1,2 and 4*/
for (i=1; i< 5;i++){

subs[i]=0;
}

while(!feof(tfPtr))
{
for (i=0; i<16; i++)
{
fscanf(filename, "%d", t[i]);
fscanf(filename, "%c", dump);
}

while(t[SIZE-16]>0){

subs[1]=1;
subs[4]=1;
label=0;
t[SIZE-16]--;
}


}
fclose(tfPtr);

exit(0);

return 0;
}

./test.c a.txt gives me Segmentation Fault(Core Dump)
For your information, my a.txt looks like:-
0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 ,1,0,1,0
.........
........
....
# 4  
Old 03-20-2008
How are you invoking the compiled executable. Show the command and all the arguments that you are passing to it.
# 5  
Old 03-20-2008
Hi,

I am executing just by ./binary.exe testfile.txt


where testfile.txt is the filename (contain the number of bits) in the binary.c (2nd argument) passed through argv.
# 6  
Old 03-20-2008
A couple things.
Code:
while(!feof(tfPtr))
{
for (i=0; i<16; i++)
{
fscanf(filename, "%d", t[i]);
fscanf(filename, "%c", dump);
}

Word to the wise:
If fscanf() doesn't read the data specified it won't advance the file pointer.
You are going to see some very undesirable behavior if you specify an incorrectly formatted file. You also should do bounds checking and add debugging code when reading from a datasource and the user. It's much easier than hoping it works.

Secondly fscanf()'s first argument is pointer to type FILE but you are passing an array of type char (filled from argv[2]). Thirdly you are not passing the address of a pointer to type int as third argument to fscanf. That's probably your segfault.

HTH


...
# 7  
Old 03-20-2008
Hi,

I edited to

Code:
strcpy(filename,argv[2]);

tfPtr = fopen(filename,"r");
       if (tfPtr == NULL)
       {
          printf("Can't find input file %s",filename);
          exit(-1);
       }


/*initialization for statement 1,2 and 4*/
for (i=1; i< 5;i++){

   subs[i]=0;
}

while(!feof(tfPtr))
{
	for (i=0; i<16; i++)
	{
		fscanf(tfPtr, "%d", &nt[i]);
		fscanf(tfPtr, "%c", &dump);
	}

Yet it still gives me Seg Faults.Is there any sample or easier way to tokenize the numbers into an array somehow?*scratching my head*
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in extracting data using cut/awk command

Hi Everyone, I have a very simple problem and i am stuck in that from last 8 days. I tried many attempts, googled my query but all in vain. I have a text file named "test.txt" In that suppose i have contents like: Java: 1 Object oriented programming language 2 Concepts of Abstraction... (5 Replies)
Discussion started by: Abhijeet Anand
5 Replies

2. Shell Programming and Scripting

Need Help in extracting data from XML File

Hi All My input file is an XML and it has some tags and data rows at end. Starting of data rows is <rs:data> and ending of data rows is </rs:data>. Within sample data rows (2 rows) shown below, I want to extract data value after equal to sign (until space or "/" sign). So if XML data... (7 Replies)
Discussion started by: vx04
7 Replies

3. Shell Programming and Scripting

Extracting data blocks from file

Hi all, I want to extract blocks of data from a file depending on the contents of that block. The input file(table) has several blocks each starting with 'gene' in the first column. I want to extract only those blocks which do not have the expression '_T02' in the second column. Input file ... (3 Replies)
Discussion started by: newbie83
3 Replies

4. UNIX for Dummies Questions & Answers

Extracting data from file

I am trying to compare the data in lines 3 & 5 to see if they match up to the '-S570' (see first code set, all proprietary information has been removed from code set) spawn telnet Trying ... Connected to CA-LOS1234-ASE-S570.cl . Escape character is '^]'. CA-LOS1234-ASE-S570 Username: ... (1 Reply)
Discussion started by: slipshft
1 Replies

5. UNIX for Dummies Questions & Answers

Efficient way of extracting data from file

I am having a file, around 500 lines. which contains one letter words, two letters words,...and so on(up to 15 letter words and words are not seprated by line). I need to compare all 1 letter words with 3,4,5 and 6 letters word, all 2 letters words with 2,3,4 and 5 letters words and all 3 letters... (3 Replies)
Discussion started by: akhay_ms
3 Replies

6. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

7. Shell Programming and Scripting

extracting data from a .csv file

I have a .csv file equipment,bandtype abc,aws def,mmds ghi,umts jkl,mmds I can get the equipment from `hostname`. In my script i want to check what is the hostname. then see if it exists in the.csv file. if it does then i want to store the second parameter(bandtype) for the corresponding... (3 Replies)
Discussion started by: lassimanji
3 Replies

8. UNIX for Dummies Questions & Answers

Extracting Data from a File

Hi I need to calculate the number of occurrences of a item in a number of files using Perl. The item appears continually throughout the files but in each case I only want to calculate it in certain blocks of the file. Example - Calculalte the number of occurrences of a 'pass' in a block of... (0 Replies)
Discussion started by: oop
0 Replies

9. Shell Programming and Scripting

Extracting Data from xml file

Hi ppl out there... Can anyone help me with the shell script to extract data from an xml file. My xml file looks like : - <servlet> <servlet-name>FrontServlet</servlet-name> <display-name>FrontServlet</display-name> ... (3 Replies)
Discussion started by: nishana
3 Replies

10. UNIX for Dummies Questions & Answers

extracting recursive data file

Hi Gurus, Can awk be able to do this source file: 1|SPFE2027G1|1PFE-7000|T34801188|5066-0844| 2|T34801188|5066-0844|T35002355|5066-0845| 3|T35002355|5066-0845|T35203409|QFBR-7798| 1|SPFE2027H1|1PFE-7000|T34801198|5066-0844| 2|T34801198|5066-0844|T35002365|5066-0845| formatted into:... (1 Reply)
Discussion started by: bbeugie
1 Replies
Login or Register to Ask a Question