Segmentation fault in c program,use malloc (Linux os) - c

this c program works in Windows But Getting " Segmentation fault (core dump)" in Linux. I gues couse of error pointers or malloc function.I can't return struct array without pointers and malloc.
struct team {
char name[12];
int m_count;
int score;
};
struct team *teamName(){
FILE *fp;
fp=fopen("teams.txt", "r");
struct team *items;
items= (struct team *) malloc(sizeof(struct team) * 10);
int i;
for(i=0; i<10; i++)
{
fscanf(fp, "%s\n" , items[i].name);
}
fclose(fp);
return items;
}
int main(void)
{ struct team *items = teamName();
getMatch(items);
}

There several problems in your code :
you do not check fopen success
you do not check fscanf success, and if a read name is greater than 11 you write out of the buffer with an undefined behavior
why the \n in the fscanf format ?
if you read less that 10 names some entries are not set, with the risk later to have an undefined behavior
A proposal taking my remarks into account can be :
#include <stdio.h>
#include <stdlib.h>
struct team {
char name[12];
int m_count;
int score;
};
struct team *teamName(){
FILE *fp = fopen("teams.txt", "r");
if (fp == NULL)
return NULL;
struct team *items = malloc(sizeof(struct team) * 10);
int i;
for (i=0; i<10; i++)
{
if (fscanf(fp, "%11s" , items[i].name) != 1) {
/* empty other names */
do {
items[i].name[0] = 0;
}
while (++i != 10);
break;
}
}
fclose(fp);
return items;
}
int main(void)
{
struct team *items = teamName();
if (items != NULL) {
for (int i = 0; i != 10; ++i) {
if (items[i].name[0] != 0)
puts(items[i].name);
}
}
/* getMatch(items); */
}
Compilation and execution :
pi#raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall m.c
pi#raspberrypi:/tmp $ cat teams.txt
aze qsd
loop
bar
pi#raspberrypi:/tmp $ ./a.out
aze
qsd
loop
bar
pi#raspberrypi:/tmp $
Note the fscanf read words, I mean a name must not contain space, else you need to use for instance fgets

Related

How can i automatically increase memory allocation for a struct in C?

I am building a small program that takes name and age as input (stored in a struct) and spits out the output. One of the problems that I am facing is I have to enter the number of people I am going to store, something that I am sure I can solve with realloc() it's just not working. Here is what I got so far.
#include <stdio.h>
#include<stdlib.h>
struct info
{
int age;
char name[30];
};
int main()
{
struct info *Ptr;
int i, num;
printf("Enter number of people");
scanf("%d", &num);
// Allocates the memory for num structures with pointer Ptr pointing to the base address.
Ptr = (struct info*)malloc(num * sizeof(struct info));
for(i = 0; i < num; ++i)
{
printf("Enter name and age:\n");
scanf("%s %d", &(Ptr+i)->name, &(Ptr+i)->age);
}
for(i = 0; i < num ; ++i)
printf("Name = %s, Age = %d\n", (Ptr+i)->name, (Ptr+i)->age);
return 0;
}
I have tried to realloc inside the first for loop, but it wasn't working even if it makes sense to have it there. Have also tried to convert the loop to a while loop like this:
while(input != "stop)
{
allocate more memory
}
How can I use realloc to in order to skip having to enter the persons number before entering them?
realloc is the correct way. Just start with Ptr = NULL and num = 0 and on each input increase the number of elements by one.
Remember to limit the number of characters scanf can read, otherwise you may buffer overrun.
Also I find Ptr[i] way easier then (Ptr+i)->.
Also compare strings with strcmp not using !=. The != will compare pointers to strings, not strings themselves.
As I like reading the whole line, then scanning the line, I would do it like this:
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
struct info
{
int age;
char name[30];
};
int main()
{
struct info *ptr = 0;
size_t num = 0;
for (;;) {
printf("Enter name and age. If you want to stop, type only 'stop'.\n");
char line[256];
if (fgets(line, sizeof(line), stdin) == NULL) {
fprintf(stderr, "fgets error");
exit(-1);
}
if (!strcmp("stop\n", line)) {
break;
}
struct info tmp;
if (sscanf(line, "%29s %d\n", tmp.name, &tmp.age) != 2) {
fprintf(stderr, "error parsing line\n");
exit(-1);
}
ptr = realloc(ptr, (num + 1) * sizeof(*ptr));
if (ptr == NULL) {
fprintf(stderr, "error allocating memory!\n");
exit(-1);
}
ptr[num] = tmp;
++num;
}
for (size_t i = 0; i < num ; ++i) {
printf("Name = %s, Age = %d\n", ptr[i].name, ptr[i].age);
}
free(ptr);
return 0;
}
If you are not sure of the no.of.elements you want to allocate and do it based on the users choice, then you can follow the below approach.
It starts with one element and the memory is reallocated as when the user wants to add new element.
#include <stdio.h>
#include<stdlib.h>
struct info
{
int age;
char name[30];
};
int main()
{
struct info *Ptr=NULL;
int i=0, num;
char c='Y';
while(c=='Y'||c=='y') {
Ptr=realloc(Ptr,(i+1)*sizeof(struct info));
if(Ptr==NULL)
break;
printf("Enter name and age:\n");
scanf("%s %d",&Ptr[i].name,&Ptr[i].age);
printf("Do you want to cont?\n");
scanf(" %c",&c);
i++;
}
num=i;
for(i = 0; i < num ; ++i)
printf("Name = %s, Age = %d\n", (Ptr+i)->name, (Ptr+i)->age);
free(Ptr);
return 0;
}
To answer exactly, you can first read the input to temp variables and check if you need to stop: Break the loop if so. Or continue and reallocate the 'storage' array by increasing it's size by one and copying the values you just read to the 'storage' array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct info
{
int age;
char name[30];
};
int main()
{
struct info * infos = 0;
int num = 0;
char input_name[30];
int input_age;
while (1)
{
printf("Enter name and age:\n");
int r = scanf("%29s", input_name);
if (r == EOF || strcmp(input_name, "stop") == 0)
break;
scanf(" %d", &input_age);
infos = realloc(infos, sizeof(struct info) * (num + 1));
infos[num].age = input_age;
memcpy(infos[num].name, input_name, sizeof(char) * 30);
num++;
}
for(int i = 0; i < num ; ++i)
printf("Name = %s, Age = %d\n", infos[i].name, infos[i].age);
return 0;
}
You should use a data struct like vector.
vector_init init vector.
vector_push push val to vector, if necessary, will realloc memory.
vector_output output the vector.
The following code could work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INIT_SIZE 1
struct info
{
int age;
char name[30];
};
struct vector {
struct info* p;
int n;
int index;
};
void vector_init(struct vector* ve) {
ve->n = INIT_SIZE;
ve->index = 0;
ve->p = malloc(sizeof(struct info) * ve->n);
}
void vector_push(struct vector* ve, struct info* tmp) {
if (ve->n == ve->index) {
ve->n *= 2;
ve->p = realloc(ve->p, sizeof(struct info) * ve->n);
}
ve->p[ve->index++] = *tmp;
}
void vector_output(const struct vector* ve) {
for (int i = 0; i < ve->index; ++i)
printf("Name = %s, Age = %d\n", ve->p[i].name, ve->p[i].age);
}
int main()
{
struct vector ve;
vector_init(&ve);
for (;;) {
struct info tmp;
printf("Enter name and age:\n");
scanf("%29s", tmp.name);
if (strcmp(tmp.name, "stop") == 0)
break;
scanf("%d", &tmp.age);
vector_push(&ve, &tmp);
}
vector_output(&ve);
return 0;
}

Saving a string from a text file to a struct using fscanf (C)

Sample Text file:
234765 PETER
867574 SMITH
I'm trying to take the id and string from the text file and save it into a struct. The id is saving fine but the string isn't.
typedef struct student
{
int id[DATA_SIZE];
char *student[DATA_SIZE];
}studentinfo;
studentinfo list;
struct student *create_space(int size)
{
struct student *tmp = (struct student*)malloc(size*sizeof(struct student));
return(tmp);
}
struct student * readData(struct student*pointer,studentinfo v)
{
int count =0;
int tmpid;
char str[256];
FILE* in_file;
in_file = fopen("studentlist.txt","r");
while(fscanf(in_file,"%d",&tmpid)!= EOF && count<DATA_SIZE)
{
fscanf(in_file,"%s",v.student[count]);
//printf("%s\n",str );
v.id[count]=tmpid;
count++;
}
pointer =&v;
return pointer;
}
int main()
{
struct student *data;
struct student *sdata;
data = create_space(1);
sdata = readData(data,list);
//printf("%s\n",sdata->student[2] );
}
Their are a couple of issues:
fscanf() reads formatted input, and returns the number of items read.
This line:
while(fscanf(in_file,"%d",&tmpid)!= EOF && count<DATA_SIZE)
Could be this:
while (count < DATA_SIZE && fscanf(in_file, "%d %255s", &list.id[count], str) == 2) {
Which verifies that 2 values are being read on each line successfully.
You are not checking if in_file returns NULL. It's safe to do this. This goes the same for malloc().
You need to correctly create space for char *students[DATA_SIZE], as this is an array of char * pointers. Once you allocate space for this via malloc() or strdup(), then you can copy the contents into students.
Here is an example of doing such a thing:
while (count < DATA_SIZE && fscanf(in_file, "%d %255s", &list.id[count], str) == 2) {
/* allocate space for one student */
list.student[count] = malloc(strlen(str)+1);
if (!list.student[count]) {
printf("Cannot allocate string\n");
exit(EXIT_FAILURE);
}
/* copy it into array */
strcpy(list.student[count], str);
count++;
}
Here is an example that you can use to help achieve your desired result:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DATA_SIZE 256
typedef struct {
int id[DATA_SIZE];
char *student[DATA_SIZE];
} studentinfo_t;
int main(void) {
FILE *in_file;
studentinfo_t list;
char str[DATA_SIZE];
size_t count = 0;
in_file = fopen("studentlist.txt", "r");
if (!in_file) {
fprintf(stderr, "%s\n", "Error reading file");
exit(EXIT_FAILURE);
}
while (count < DATA_SIZE && fscanf(in_file, "%d %255s", &list.id[count], str) == 2) {
list.student[count] = malloc(strlen(str)+1);
if (!list.student[count]) {
printf("Cannot allocate string\n");
exit(EXIT_FAILURE);
}
strcpy(list.student[count], str);
count++;
}
for (size_t i = 0; i < count; i++) {
printf("%d %s\n", list.id[i], list.student[i]);
}
return 0;
}

C Programming: Reading a file and storing in array of struct

I am trying to read a file test.txt via fscanf and store it in a array of struct. This is what I tried. Problem here is that fscanf is not working as it is supposed to. After reading the file, I am also trying to print it on screen, but it won't work.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Item {
double value;
int unit;
char name[50];
};
int load(struct Item* item, FILE* data);
void display(struct Item item, int variableA);
int main()
{
struct Item I;
int i;
char ck;
ck = fopen("test.txt", "r");
if (ck)
{
for (i = 0; i < 3; i++)
{
load(&I, ck);
display(I, 0); //DISPLAY FUNCTION THAT READS test.txt and DISPLAYS
}
fclose(ck);
}
return 0;
}
int load(struct Item* item, FILE* data)
{
fscanf(data, "%d,%.2lf,%s\n", &(*item).unit,&(*item).value,&(*item).name);
return 0;
}
void display(struct Item item, int variableA)
{
printf("|%3d |%12.2lf| %20s |***\n", item.unit, item.value, item.name);
return;
}
This is what I have in test.txt file:
205,11.20,John Snow
336,23.40,Winter is coming
220,34.20,You know nothing
Error: Program compiles with some warnings , but I get segmentation fault when I execute the code.
Any idea why?
Output Expectation: OUTPUT should be read from test.txt file and should be displayed on to the screen.
Multiple problems in the program:
1.
char ck;
ck = fopen("test.txt", "r");
fopen returns a FILE*, not a char, use
FILE* ck = fopen(...);
2.
fscanf(data, "%d,%.2lf,%s\n", &(*item).unit,&(*item).value,&(*item).name);
always check return value of fscanf, if it is smaller than the number of fields you requested, the following call to fscanf is unlikely to do what you expect. Also, *item.unit is the same as item->unit, use item->unit because it is shorter and cleaner:
int ret = fscanf(data, "%d,%lf,", &item->unit, &item->value);
if (ret != 3) { // error }
Third, %s matches a sequence of non-white-space characters, so when fscanf reads "John", it will stop, and the next fscanf call will get to read "Snow" while expecting an integer.
So to input a string with whitespace, use fgets instead, and remember to remove the newline character in the end.
Try following:
int main(void)
{
struct Item I;
int i;
FILE* ck;
int ret;
ck = fopen("test.txt", "r");
if (ck)
{
for (i = 0; i < 3; i++)
{
ret = load(&I, ck);
if (ret < 0)
break;
display(I, 0); //DISPLAY FUNCTION THAT READS test.txt and DISPLAYS
}
fclose(ck);
}
return 0;
}
int load(struct Item* item, FILE* data)
{
int ret = fscanf(data, "%d,%lf,", &item->unit, &item->value);
if (ret != 2) {
return -1;
}
fgets(item->name, sizeof item->name, data);
item->name[strlen(item->name)-1] = '\0';
return 0;
}
void display(struct Item item, int variableA)
{
printf("|%3d |%12.2lf| %20s |***\n", item.unit, item.value, item.name);
return;
}
It outputs:
$ ./a.out
|205 | 11.20| John Snow |***
|336 | 23.40| Winter is coming |***
|220 | 34.20| You know nothing |***
You can try this different approach.
It uses:
malloc,realloc to allocate and reallocate memory for array of structs. I assumed that much larger text files with more lines will be used and this allows the array to resize when needed to accommodate more information.
strtok to parse each peice of data between , delimeters, and then store them into the array of structures.
Checks return value of pointers to avoid segmentation faults.
Uses fgets to read each line of the file into a string, from which we can parse ourselves afterwards.
This is the proposed code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAMESTRLEN 50
#define INITSIZE 3
#define MAXSIZE 100
typedef struct {
int unit;
double value;
char name[NAMESTRLEN+1];
} item_t;
typedef struct {
item_t *items;
int numlines;
} allitems_t;
allitems_t *initialize_arraystructs(void);
void print_arraystructs(allitems_t *allitems);
void read_insert_items(FILE *filestream, allitems_t *allitems);
void check_ptr(void *ptr, const char *msg);
int
main(void) {
allitems_t *allitems;
FILE *fp;
fp = fopen("test.txt", "r");
if (fp == NULL) {
fprintf(stderr, "%s\n", "Error reading file!\n");
exit(EXIT_FAILURE);
}
allitems = initialize_arraystructs();
read_insert_items(fp, allitems);
print_arraystructs(allitems);
return 0;
}
void
read_insert_items(FILE *filestream, allitems_t *allitems) {
int count = 0;
char line[MAXSIZE];
char *unit, *value, *name;
size_t numitems = INITSIZE;
allitems->items = malloc(numitems * sizeof(item_t));
check_ptr(allitems->items, "Initial Allocation");
while (fgets(line, MAXSIZE, filestream) != NULL) {
unit = strtok(line, ",");
value = strtok(NULL, ",");
name = strtok(NULL, "\n");
if (count == numitems) {
numitems *= 2;
allitems->items = realloc(allitems->items, numitems * sizeof(item_t));
check_ptr(allitems->items, "Reallocation");
}
allitems->items[count].unit = atoi(unit);
allitems->items[count].value = atof(value);
strcpy(allitems->items[count].name, name);
count++;
allitems->numlines++;
}
}
allitems_t
*initialize_arraystructs(void) {
allitems_t *allitems;
allitems = malloc(sizeof(allitems_t));
check_ptr(allitems, "Initial Allocation");
allitems->items = NULL;
allitems->numlines = 0;
return allitems;
}
void
print_arraystructs(allitems_t *allitems) {
int i;
for (i = 0; i < allitems->numlines; i++) {
printf("%d,%.2f,%s\n",
allitems->items[i].unit,
allitems->items[i].value,
allitems->items[i].name);
}
}
void
check_ptr(void *ptr, const char *msg) {
if (!ptr) {
printf("Unexpected null pointer: %s\n", msg);
exit(EXIT_FAILURE);
}
}

How to read a specific amount of lines from a file in C

My question is that I am trying to read only a certain amount of files given n files.
For example, I have the two files with the following content inside then
test1:
A cat ran off
Apple
test2:
The boy went home
Apples are red
I want the output to be
test1: A cat ran off
Not
test1: A cat ran off
test2: Apples are red
This is the code that I have written so far:
#include <stdio.h>
#include <string.h>
int main (int argc, char ** argv)
{
extern int searcher(char * name, char*search,int amount);
while(argc != 0){
if(argv[argc] !=NULL)
if(searcher(argv[argc],"a",1)) break;
argc-- ;
}
}
int searcher(char * name, char*search,int amount){
FILE *file = fopen (name, "r" );
int count = 0;
if (file != NULL) {
char line [1000];
while(fgets(line,sizeof line,file)!= NULL && count != amount)
{
if(strstr(line,search) !=NULL){
count++;
if(count == amount){
return(count);
}
printf("%s:%s\n", line,name);
}
}
fclose(file);
}else {
perror(name); //print the error message on stderr.
}
return(0);
}
Continuing from the comments, and noting you will need to remove the trailing newline included by fgets, you could do something like the following:
#include <stdio.h>
#include <string.h>
enum { MAXC = 1000 };
int searcher (char *name, char *search, int amount);
void rmlf (char *s);
int main (int argc, char **argv)
{
int i;
for (i = 1; i < argc; i++)
if (searcher (argv[i], "a", 1))
break;
return 0;
}
int searcher (char *name, char *search, int amount)
{
FILE *file = fopen (name, "r");
int count = 0;
if (!file) {
fprintf (stderr, "error: file open failed '%s'.\n", name);
return 0;
}
char line[MAXC] = "";
while (count < amount && fgets (line, MAXC, file)) {
rmlf (line); /* strip trailing \n from line */
if (strstr (line, search)) {
count++;
printf ("%s: %s\n", name, line);
}
}
fclose (file);
return count == amount ? count : 0;
}
/** stip trailing newlines and carraige returns by overwriting with
* null-terminating char. str is modified in place.
*/
void rmlf (char *s)
{
if (!s || !*s) return;
for (; *s && *s != '\n'; s++) {}
*s = 0;
}
Example Input Files
$ cat test1
A cat ran off
Apple
$ cat test2
The boy went home
Apples are red
Example Use/Output
You understand iterating with argc-- your files are processed in reverse, so you will end up with output like:
$ ./bin/searcher test2 test1
test1: A cat ran off
$ ./bin/searcher test1 test2
test2: Apples are red
note: to process the files in order, just do something like for (i = 1; i < argc; i++) instead of while (argc--). Let me know if you have further questions.
Changing to the for loop instead of the while in main and inputting 10 as the number of occurrences to look for, all files are processed, e.g.:
$ ./bin/searcher test1 test2
test1: A cat ran off
test2: Apples are red

Valgrind invalid write of size 8

I'm experimenting with C structs and I've come up with a invalid write of size 8 followed by invalid read of size 8 messages from valgrind.
My code is only looping through arguments (if argc > 1) and for each filename, it scans for a string and unsigned int indicating name and age(struct player).
This is all the code I've got so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct player {
char name[20];
unsigned int age;
};
struct player *player_new_from_stream(FILE * stream){
struct player *new_player = (struct player*) malloc(sizeof(struct player));
char *p_name = malloc(20);
char *p_age = malloc(20);
if (stream != stdin){
if (fgets(p_name, 20, stream) != NULL){
char *p = strrchr(p_name, '\n');
if (p)
*p = '\0';
strcpy(new_player->name, p_name);
}
if (fgets(p_age, 20, stream) != NULL)
new_player->age = atoi(p_age);
}
else {
printf("enter name and age for a player\n");
gets(p_name);
gets(p_age);
strcpy(new_player->name, p_name);
new_player->age = atoi(p_age);
}
free(p_name);
free(p_age);
return new_player;
}
void player_inspect(struct player plyr, char* prefix){
printf("[%s] name: %s\n", prefix, plyr.name);
printf("[%s] age : %d\n", prefix, plyr.age);
}
int main(int argc, char* argv[]){
FILE * stream;
char* argument;
// below: trying to allocate (argc - 1) pointers
// valgrind's --show-origins=yes points here for both errors
struct player **players = malloc(sizeof(int) * (argc - 1));
int i = 1;
for (; i < argc; i++){
argument = argv[i];
if (strcmp("-", argument) != 0){
if ((stream = fopen(argument, "r")) == NULL) perror("Error opening file");
else {
// the next line emits Invalid write of size 8 in valgrind
players[i-1] = player_new_from_stream(stream);
fclose(stream);
}
} else {
players[i-1] = player_new_from_stream(stdin);
}
}
i = 0;
char buffer[15];
for (; i < argc - 1; i++){
sprintf(buffer, "%d", i);
// the next line emits Invalid read of size 8
player_inspect(*(players[i]), buffer);
free(players[i]);
}
free(players);
return 0;
}
What is wrong here? I want to return a pointer to struct player from player_new_from_stream and pack this pointer to array players in main().
This is wrong:
struct player **players = malloc(sizeof(int) * (argc - 1));
Use this instead:
struct player **players = malloc(sizeof(*players) * (argc - 1));
Note that on your system, sizeof(int) == 4 while sizeof(struct player *) == 8.
I ran it under valgrind with valid input files (player files), compiled with gcc -g and it didn't give any of these invalid read/write messages.
It also worked for using stdin.
However, when I ran it with non-existent files, it had a read error at
i = 0;
char buffer[15];
for (; i < argc - 1; i++){
sprintf(buffer, "%d", i);
player_inspect(*(players[i]), buffer); // <<HERE
free(players[i]);
}
Since the players[i] pointer was NULL due to the pointer at that array index not being set if the fopen call fails.
You need to do double allocation if you want to use array:
struct player **players = malloc(sizeof(struct player*) * (argc - 1));
for (int i=0; i<argc-1;i++)
player[i] = malloc(sizeof(struct player));

Resources