I have a stem file named myfile
, it has 2 sub files : myfile.data
and myfile.names
.
Now I want to write a code where it will first check the .names
file's last line and see whether there is a zero or not, if there is any other number other than zero then it will say noisy data
. And if the .names
files last line has a zero, then I need to read the .data file and do some operations on that .data
file to produce a valid output.
Now below I am giving what I have tried to do:
int main(int argc, char **argv)
{
char* path;
sprintf(path, "%s.names", argv[1]);
printf("...%s...\n", path);
//FILE *fp =stdin;
fp = fopen(path, "r");
char buff[1024];
int check = 0;
fseek(fp, 0, SEEK_SET);
while(!feof(fp))
{
memset(buff, 0x00, 1024);
fscanf(fp, "%[^\n]\n", buff);
}
if(strchr(buff, '0') != NULL){
check = 1;
}
if(check == 0){
printf("Noisy data...\n");
}
else if(check == 1){
char* path1;
sprintf(path1, "%s.data", argv[1]);
printf("...%s...", path1);
//FILE *fp1 = stdin;
fp1 = fopen(path1, "r");
char buf[1024];
if( fgets(buf, sizeof buf, fp1) == NULL ){
fputs("Input error\n", stderr);
return 1;
}
...
...
Produces the perfect output
}
It is compiling successfully.
To execute this : <./executable_filename> <stemFileName> > <outputFileName>
So whenever I am doing : ./myFileName myfile output
. It is saying Segmentation fault (core dumped)
. Why is it happening?
One more point my .names files will contain either : | Final Pseudo Deletion Count is 0.
or | Final Pseudo Deletion Count is 1.
or | Final Pseudo Deletion Count is 2.
, So I guess the strchr
will do my job is this case?
I initially tried to modify this below code, but after trying several different ways, I am not able to.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE * xfopen(const char *path, const char *mode);
void * xmalloc(size_t s);
void parse_line(const char *buf, int *max, int column_count)
{
for(int i = 0; i < column_count; i++ ){
char *end;
int t = strtol(buf, &end, 10);
if( t > max[i] ){
max[i] = t;
}
if( !((i < column_count - 1 && *end == ',') || (i == column_count - 1 && *end == '\n')) ){
fprintf(stderr, "invalid input '%c' in %s", *end, buf);
exit(1);
}
buf = end + 1;
}
}
int main(int argc, char **argv)
{
char *path1;
char *path = argc > 1 ? argv[1] : "stdin";
sprintf(path, "%s.data", argv[1]);
FILE *in = argc > 1 ? xfopen(path, "r") : stdin;
char buf[1024];
int column_count = 1;
int row_count = 1;
int *max;
/* Read first line to determine number of columns */
if( fgets(buf, sizeof buf, in) == NULL ){
fputs("Input error\n", stderr);
return 1;
}
for( const char *p = buf; *p; p++ ){
if( *p == ',' ){
column_count += 1;
}
}
max = xmalloc(column_count * sizeof *max);
for( int i = 0; i < column_count; i++ ){
max[i] = INT_MIN;
}
parse_line(buf, max, column_count);
while( fgets(buf, sizeof buf, in) != NULL ){
row_count += 1;
parse_line(buf, max, column_count);
}
if( fseek(in, 0L, SEEK_SET) ){
perror(path);
return 1;
}
printf("%d %d ", row_count, column_count - 1);
for( int i = 0; i < column_count - 1; i += 1 ){
printf("%d ", max[i]);
}
printf("%d\n", max[column_count - 1] + 1);
while( fgets(buf, sizeof buf, in) != NULL ){
char *comma = strrchr(buf, ',');
if( comma == NULL ){
fprintf(stderr, "Invalid input\n");
return 1;
}
*comma = '\0';
int k = strtol(comma + 1, NULL, 10);
for(char *p = buf; *p; p++){
if( *p == ',' ) *p = ' ';
}
printf("%s %d\n", buf, k + 1);
}
}
FILE *
xfopen(const char *path, const char *mode)
{
FILE *fp = path[0] != '-' || path[1] != '\0' ? fopen(path, mode) :
*mode == 'r' ? stdin : stdout;
if( fp == NULL ){
perror(path);
exit(EXIT_FAILURE);
}
return fp;
}
void *
xmalloc(size_t s)
{
void *rv = malloc(s);
if( rv == NULL ){
perror("malloc");
exit(EXIT_FAILURE);
}
return rv;
}