0

hello this is my first year in learning c programming , i spent all day trying to figure this one out and now i have a head ache anyway i just wanna keep this as simple as possible, here is a test code that i want to correct so i can understand how this works what i wanna do is store a dynamic array of structure variables in a text file, and then reading the data from that text file into a dynamic array. this is in c language

#include <stdio.h>
#include <stdlib.h>
struct student {
char nam[3];      // we store to this 
char testname[3]; // we read to this
}*science[10];
int main() {
int i;
FILE *ptr=fopen("science_class","a");
for (i=0;i<3;i++){          //storing the infro from dynamic array into the file
e[i]=(science*)calloc(3,sizeof(char));
puts("enter name");
gets(science[i]->name);
fprintf(ptr,"%s",science[i]->name);  }
for (i=0;i<3;i++){          // loading the info from the file to a dynamic array
fscanf(ptr,"%s",&science[i]->testname)
printf("name :%s \n",science[i]->testname) }
fclose(ptr);

}

1 Answer 1

2

I wrote a simple c program that has a car struct. The program simply reads data about cars into a dynamic array and writes it back to another file. Hope you understand the concepts:

#include<stdio.h>
#include<stdlib.h>

struct car
{
    char name[20];
    char color[20];
    float mass;
    int price;
};
typedef struct car Cars;

int main()
{
    int i, n;
    Cars *cars;
    ///////// READ:
    FILE *in = fopen("cars_in.txt", "r");
    fscanf(in, "%i", &n); //read how many cars are in the file
    cars = (Cars*)malloc(n*sizeof(Cars)); //allocate memory

    for (i = 0; i < n; ++i) //read data
    {
        fscanf(in, "%s", cars[i].name);
        fscanf(in, "%s", cars[i].color);
        fscanf(in, "%f", &cars[i].mass);
        fscanf(in, "%i", &cars[i].price);
    }

    fclose(in);


    ///////////// WRITE:
    FILE *out = fopen("cars_out.txt", "w");
    fprintf(out, "%d\n", n); 
    for (i = 0; i < n; ++i) 
    {
        fprintf(out, "%s ", cars[i].name);
        fprintf(out, "%s ", cars[i].color);
        fprintf(out, "%f ", cars[i].mass);
        fprintf(out, "%i\n", cars[i].price);
    }

    fclose(out);
    free(cars);
    return 0;
}

and here is some data that you should put in a cars_in.txt:

5
BMW red 1500 80000
Opel black 950 15000
Mercedes white 2500 100000
Ferrari red 1700 2000000
Dodge blue 1800 750000

EDIT: I simply changed fscanf to scanf and works fine. Be careful when you enter the data: first you have to tell how many cars you want to add so malloc can reserve space, after you enter the car's name->color->mass->price separated with white characters (enter, space, tab). Just change the read section, the rest of the code remains the same:

///////// READ FROM KEYBOARD

scanf("%d", &n); // first we have to know the number of cars

cars = (Cars*)malloc(n*sizeof(Cars)); //allocate memory

for (i = 0; i < n; ++i) //read data -> be careful: you have to keep the order
{
    scanf("%s", cars[i].name);
    scanf("%s", cars[i].color);
    scanf("%f", &cars[i].mass);
    scanf("%i", &cars[i].price);
}
Sign up to request clarification or add additional context in comments.

3 Comments

what this one does is that it takes the content of the first file, and it copies it on to the second file, i replaced the first fscanf lines with this scanf("%s", cars[i].name); scanf("%s", cars[i].color); scanf("%f", &cars[i].mass); scanf("%i", &cars[i].price); so i can manualy input data but the program cruchs after i was done typing them in which means i still cant store data in a text file coould u please help me with that
sorry i replied so late, and hope you understand, i don't speak English very well
sorry i just got the chance to reply , thank you so much for the help, it helped me make sense of how this works, im very greatful for ur help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.