0
I have a Function that returns a string from a row of a file, but when I run it I get a message from Segmentation fault. Here’s the code:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "parser.h"
const int MAX_CHAR_PER_LINE = 100;
int main()
{
FILE *h = fopen("/home/test.txt", "r");
char *line = read_line(h);
printf("%s", line);
return 0;
}
int parse(FILE *header)
{
return 0;
}
char* read(FILE *header, int bytes)
{
char *ret_v = (char*) malloc(bytes);
fread((void*) ret_v, 1, bytes, header);
return ret_v;
}
char* read_line(FILE *header)
{
char *line = (char*) malloc(MAX_CHAR_PER_LINE);
char ch = getc(header);
int count = 0;
while (ch != 255 && ch != '\n')
{
line[count] = ch;
ch = getc(header);
count++;
}
line[++count] = '\0';
char *ret_line;
memcpy(ret_line, line, strlen(line));
free(line);
return ret_line;
}
I don’t understand why this mistake is happening, apparently I did everything right. : / Note: I just discovered that the "error" is not in the code, but in the compiler. I compiled with TCC and the segfault error appeared, but when I compiled with gcc Function returned the correct value.
But if the goal is to read the entire file and put into one
char *
, because you didn’t just define this infread
and usedfseek
to know the size of therewind
to return the pointer to the beginning before using thefread
? ... Of course, if the file is large, I think it’s not worth it, it would be better to read with limits and print on the screen (which seems to be the goal)– Guilherme Nascimento
Yes the file will be great, that’s why I want to read line by line.
– Carlos Henrique
So if you want to read line by line is doing this "wrong" (from my point of view), allocating a large file in memory seems like a bad strategy, I have to understand your goal to at least try to indicate a strategic solution of how to do this. Simply reading by reading does not seem the real purpose of your application, if you can give an idea of what you want to do with the data read I can try to point a way.
– Guilherme Nascimento
I just "solve the problem", my goal is to extract functions within the headers to soon create an IDE for C.
– Carlos Henrique
@Carloshenrique answer and ask and accept, it is better to leave the record.
– Maurício Z.B
So @Carloshenrique can do this without throwing everything into memory, you can type create a Token identifier (assuming you are creating a "language of your own"), the Tokens would be reserved words of their functions that we could detect at each loop and only allocate in memory what is important ... seriously you do not need to allocate the entire file in memory, this will kill your application, or at least give CRASH.
– Guilherme Nascimento
I understand, but in case I would allocate in memory only one line, and from that line would identify the tokens
– Carlos Henrique
So, but the error is probably occurring in your script because it did this, allocated everything at once, what you need to do is what I commented earlier, read piece by piece and only allocate what interests you.
– Guilherme Nascimento
Could you send me an example?
– Carlos Henrique
How is the content format of this your . txt?
– Guilherme Nascimento
Text format, ASCII text
– Carlos Henrique
Like
char
only stores a single byte (and that byte has signal bit), I believe the comparisonch != 255
will not return what you want. By the way, for reasons of optimization, one reads a row of bytes at once, instead of always going in the file to pull a new character. Not that that’s wrong, it’s just unnecessarily slow.– Jefferson Quesado