#Why does this happen?

12 messages · Page 1 of 1 (latest)

torpid kelp
#

I feel like there's an easy fix on this, but I can't figure it out

dawn waveBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

torpid kelp
#

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#define strnlen(s,n) strlen(s)

char line[30];
char *buffer[12];
FILE *Input, *Output;
uint8_t counter = 0;
int val;
int sizeOf;
Input = fopen("input_file.dat", "r");
Output = fopen("output_file.dat", "w");
while(fgets(line, 50, Input) != NULL)
{
sizeOf = (strlen(line)+1);
buffer[counter] = (char )malloc(sizeOf);
strcpy(buffer[counter],line);
val = atoi(buffer[counter]);
if (counter == 0)
{
fprintf(Output,"%d\n",val);
}
else{
fprintf(Output,"%d\n", val
val);
}
counter++;
}
fclose(Input);
fclose(Output);
free(buffer[counter]);

#

Run error
Segmentation fault (core dumped)

#

The instructions:

Implement a function, that will open a file named input_file.dat for reading, read it line by line, calculate new values according to the following rules and output the modified version to a file named output_file.dat.

  • The first line of the input file contains the amount of data points, contained by the file. The value stored in this line should be copied into the output file as is.

  • Each consequent line contains a single data point as a single integer number, followed immediately by a newline character. For these lines the value written into the output file should be computed as a square of the input value.

hardy quail
#

@torpid kelp
I don't remember but maybe you are using wrong malloc...

I think you should reserve memory like this

char *buffer = (char *) malloc(sizeOf * sizeof(char));
// some code here
free(buffer)

sizeof belongs to stdlib.h if I'm not wrong

shell pecan
#

At the end you free buffer[counter]
But because of how counter increments buffer[counter] doesn't hold a pointer from malloc

#

Not sure of that is the whole issue, but it is an issue

dawn waveBOT
#

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#define strnlen(s,n) strlen(s)

char line[30];
char* buffer[12];
FILE *Input, *Output;
uint8_t counter = 0;
int val;
int sizeOf;
Input = fopen("input_file.dat", "r");
Output = fopen("output_file.dat", "w");
while (fgets(line, 50, Input) != NULL) {
  sizeOf = (strlen(line) + 1);
  buffer[counter] = (char*)malloc(sizeOf);
  strcpy(buffer[counter], line);
  val = atoi(buffer[counter]);
  if (counter == 0) {
    fprintf(Output, "%d\n", val);
  } else {
    fprintf(Output, "%d\n", val * val);
  }
  counter++;
}
fclose(Input);
fclose(Output);
free(buffer[counter]);
schlongster
runic coral
#

!f.

#

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#define strnlen(s,n) strlen(s)

char line[30];
char* buffer[12];
FILE *Input, *Output;
uint8_t counter = 0;
int val;
int sizeOf;
Input = fopen("input_file.dat", "r");
Output = fopen("output_file.dat", "w");
while (fgets(line, 50, Input) != NULL) {
  sizeOf = (strlen(line) + 1);
  buffer[counter] = (char*)malloc(sizeOf);
  strcpy(buffer[counter], line);
  val = atoi(buffer[counter]);
  if (counter == 0) {
    fprintf(Output, "%d\n", val);
  } else {
    fprintf(Output, "%d\n", val * val);
  }
  counter++;
}
fclose(Input);
fclose(Output);
free(buffer[counter]);
```.