LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c file handling on linux (https://www.linuxquestions.org/questions/programming-9/c-file-handling-on-linux-196683/)

suchi_s 06-23-2004 02:09 AM

c file handling on linux
 
running the programm that is reading a file
after running its exe gives the error segmentation fault

fluppi 06-23-2004 02:40 AM

It's difficult to say something with so few infos...

Well, can you give us more details (source, what compiler, the command you used for compiling and after to run the program) ?

suchi_s 06-23-2004 02:47 AM

code is #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>
int main ()
{
char *rept_cell[900];
char cell_alarm[5800];
char temp_cell_all[5800];
char cell_alarm1[2000];
FILE *input_file, *output_alarm, *output_clear, *file_2MB;
int M_STAR;
int testptr;
char *teststr;
char *teststr1;
int STAR;
int ECP;
int AP;
int ACK_STAR;
int ACK_ECP;
int ACK_AP;
int file_pointer = 0;
int flag_2mb;
//while (1)
// {
input_file = fopen ("031209.APX", "r");
output_alarm = fopen ("rept_alarms.dat", "w+");
output_clear = fopen ("rept_clear.dat", "w+");
while (!feof (input_file))
{
M_STAR = 0;
STAR = 0;
AP = 0;
ECP = 0;
ACK_STAR = 0;
ACK_ECP = 0;
ACK_AP = 0;
fgets (rept_cell[0], 900, input_file);
printf("hi");
cell_alarm[0] = '\0';if ((strstr (rept_cell[0], "*") != NULL)
&& (strstr (rept_cell[0], "REPT:CELL") != NULL)
&& (strstr (rept_cell[0], "BOGGED") == NULL))
{
strcat (cell_alarm, rept_cell[0]);
cell_alarm[strlen (cell_alarm) - 1] = ';';
while (strstr (rept_cell[0], "/") == NULL)
{
fgets (rept_cell[0], 900, input_file);
if ((strstr(rept_cell[0],
"MEAS ERROR") == NULL) && (strstr (rept_cell[0], "CALLDROP") == NULL)&&
(strstr(rept_cell[0],"DBMISMATCH") == NULL))
STAR = 1;
{
if ((strchr (rept_cell[0], '\n') != NULL) && (strchr(rept_cell[0],'/') == NULL))
{
strcat (cell_alarm,
rept_cell[0]);
cell_alarm[strlen (cell_alarm) - 1] = ' ';
}
else if (strchr (rept_cell[0], '/') != NULL)
{
*rept_cell[0] = ';';
strcat (cell_alarm, rept_cell[0]);
}
}
}
}

if ((M_STAR == 1) || (STAR == 1) || (AP == 1))
fprintf (output_alarm, "%s", cell_alarm);
}
// }
fclose(input_file);
compiler is gcc
to run i use ./a.out

aluser 06-23-2004 07:56 AM

Code:

fgets (rept_cell[0], 900, input_file);
This is wrong because the first argument to fgets should be a char*, not char. I'm a little suprised the compiler didn't warn about that. If not, compile with -Wall.

please post code under [ code ] ... [ /code ] (without the spaces) in the future.

itsme86 06-23-2004 08:50 AM

The problem is, you're not allocating space for rept_cell[0]. You have an array of 900 char * pointers and you're only using the first index in the array. I'd either change the definition char *rept_cell[900]; to just char rept_cell[900]; and then go through with Find and Replace to change every instance of rept_cell[0] to rept_cell or allocate memory for rept_cell[0]. I don't really see a need for the array of pointers in the code you pasted though. Best to drop the * from the definition and then Find and Replace like I mentioned. The fact that your fgets() call specifies a buffer size of 900 bytes makes me think that you didn't intend to create an array of pointers in the first place anyway.

EDIT: to aluser, rept_cell[0] is a char * in the code he pasted. That's why gcc allowed it. The problem is that rept_cell[0] just points to garbage.

aluser 06-23-2004 09:01 AM

gah my bad, I read it as char rept_cell[900] .. I should wake up first before posting


All times are GMT -5. The time now is 05:36 AM.