Related
I need to solve a problem. I need to create two functions which scan user's input information and put in into structure array and then put all array to BIN file (with function fwrite()) and other function - read from BIN file with function fread(). You can see my code below. Problem is that I can write to file, but when I read I get filler array element and other element which are empty. How to get only filled struct array element?
#include <stdio.h>
#include <stdlib.h>
#include <mem.h>
typedef struct
{
char Lesson[50];
char TeachersName[50];
char TeachersLastName[50];
int Credits;
int NumberOfStudents;
} School;
void ToFile (char *fileName);
void FromFile (char *FileName);
int main()
{
char *fileName[]={"student.bin"};
ToFile (*fileName);
FromFile (*fileName);
return 0;
}
void ToFile (char *fileName)
{
int n, chars;
FILE *fp;
fp = fopen(fileName, "wb");
School Info[20];
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
printf("Testing fwrite() function: \n\n");
printf("Enter the number of records you want to enter: ");
scanf("%d", &n);
for(int i = 0;i<n;i++)
{
printf("\nEnter details of employee %d \n", i + 1);
fflush(stdin);
printf("Lesson: ");
gets(Info[i].Lesson);
printf("Teachers name: ");
gets(Info[i].TeachersName);
printf("Teachers last name: ");
gets(Info[i].TeachersLastName);
printf("Credits: ");
scanf("%d", &Info[i].Credits);
printf("Number of studens: ");
scanf("%d", &Info[i].NumberOfStudents);
chars = fwrite(&Info[i], sizeof(Info), 1, fp);
printf("Number of items written to the file: %d\n", chars);
}
fclose(fp);
}
void FromFile (char *FileName)
{
School Info[20]= { { "", "","",0,0 } };;
FILE * fpp;
fpp = fopen(FileName, "rb");
fread(&Info, sizeof(Info), 1, fpp);
/*
for(int j=0; j<20; ++j) {
printf("\nLesson: %s", Info[j].Lesson);
printf("\nTeachers name: %s", Info[j].TeachersName);
printf("\nTeachers last name: %s", Info[j].TeachersLastName);
printf("\nCredits: %d", Info[j].Credits);
printf("\nNumber of students: %d", Info[j].NumberOfStudents);
printf("\n");
}*/
int j=0;
while (fread(&Info, sizeof(Info), 1, fpp))
{
printf("\nLesson: %s", Info[j].Lesson);
printf("\nTeachers name: %s", Info[j].TeachersName);
printf("\nTeachers last name: %s", Info[j].TeachersLastName);
printf("\nCredits: %d", Info[j].Credits);
printf("\nNumber of students: %d", Info[j].NumberOfStudents);
printf("\n");
j++;
}
fclose(fpp);
}
Im currently having to take an input.txt file, where it goes something like
3
Sarah 90 40 30
John 23 55 33
help 34 99 74
as an input file,
and read it into a struct array, then create an output.txt.
I seem to be having a problem with the assignment. I tried fscanf, fgetc, fgets, strtok, delim and everything i could find on the internet, but due to my sloppy pointer knowledge, i seem to be stuck.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
typedef struct studentT{
char *name;
int literature;
int math;
int science;
}studentT;
int main(void)
{
printf("Start of main\n");
FILE *fptr;
int i;
fptr = fopen("input.txt", "r");
//reading first line to dynamically allocate studentT
printf("dynamically allocating studentT\n");
char tempsize[4];
//fgets(tempsize,1,fptr);
//i=atoi(&tempsize[0]);
fscanf(fptr,"%d",&i);
struct studentT* record = malloc(i*sizeof(*record));
printf("i has interger value %d\n", i);
//line counter ignoring line 0; reading 1 and 2.
char line[24];
char buffer[24];
char delim[] = " ";
char *array[4];
int *darray[4];
printf("entering while loop\n");
fgets(buffer,24,fptr);
int idx =0;
while(fgets(line, sizeof(line),fptr)!=NULL &&idx<i)
{
puts(line);
char *buf = strtok(line, delim);
int iter =0;
while(buf!=NULL)
{
if(iter = 0){
array[iter]=buf;
buf = strtok(NULL,delim);
}
for(iter=1;iter<4;iter++){
*darray[iter] =atoi(buf);//
printf("%d,",*darray[iter]);
buf=strtok(NULL, delim);
}
}
record[idx].name=array[0];
record[idx].literature=*darray[1];
record[idx].math=*darray[2];
record[idx].science=*darray[3];
// printf("array value: %s %d %d %d\n",array[0], array[1], array[2], array[3]);
// printf("value after casting: %s %zu %zu %zu\n", array[0], (uintptr_t)array[1], (uintptr_t)array[2], (uintptr_t)array[3]);
//sscanf(buf,"%s %d %d %d",record[idx].name, &record[idx].literature, &record[idx].math, &record[idx].science);
printf("%s\n", line);
//fscanf(fptr,"%s %d %d %d",record[idx].name, &record[idx].literature, &record[idx].math, &record[idx].science);
line[strlen(line)-1]='\0';
printf("in idx loop %d %s %d %d %d\n\n", idx, record[idx].name, record[idx].literature, record[idx].math, record[idx].science);
idx++;
}
//output of file
printf("output commencing\n\n\n");
int tempave;
FILE *fout;
char *str1 = "Name Literature Math Science Ave.\n";
char *str2 = "Ave. ";
char *str3 = "Sarah\t 96\t 90\t 80\t 88.67";
char *str4 = "Minsu\t 55\t 70\t 76\t 67.00";
char *str5 = "Nara\t 88\t 70\t 96\t 84.67";
char *str6 = "79.67 76.67 84.00 80.11";
fout = fopen("output.txt", "w");
fprintf(fout,"%s\n", str1);
for(int idx=0;idx<i;idx++){
tempave = (record[idx].math+record[idx].literature+record[idx].science)/3;
fwrite(&record[idx],sizeof(struct studentT),1,fout);
fprintf(fout, "%d\n",tempave);
}
//fprintf(fout, "%s\n %s\n %s\n %s %s\n",str3,str4,str5,str2,str6);
fprintf(fout,"%s",str2);
float mathave,litave,sciave;
for(int idx=0;idx<3;idx++){
mathave+=record[idx].math;
litave+=record[idx].literature;
sciave+=record[idx].science;
}
mathave=mathave/3;
sciave=sciave/3;
litave=litave/3;
fprintf(fout,"%f %f %f",litave, mathave, sciave);
fclose(fptr);
fclose(fout);
}
edit: colleague mentioned I should malloc arrays so the code now looks uglier:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
typedef struct studentT{
char *name;
int literature;
int math;
int science;
}studentT;
int main(void)
{
printf("Start of main\n");
FILE *fptr;
int i;
fptr = fopen("input.txt", "r");
//reading first line to dynamically allocate studentT
printf("dynamically allocating studentT\n");
char tempsize[4];
//fgets(tempsize,1,fptr);
//i=atoi(&tempsize[0]);
fscanf(fptr,"%d",&i);
struct studentT* record = malloc(i*sizeof(*record));
printf("i has interger value %d\n", i);
//line counter ignoring line 0; reading 1 and 2.
char line[24];
char buffer[24];
char delim[] = " ";
char *array[4];
int darray[4];
printf("entering while loop\n");
fgets(buffer,24,fptr);
int idx =0;
while(fgets(line, sizeof(line),fptr)!=NULL &&idx<i)
{
puts(line);
char *buf = strtok(line, delim);
int iter =0;
while(buf!=NULL)
{
if(iter = 0){
array[iter] = malloc(sizeof(char)*strlen(buf));
strcpy(array[iter],buf);
buf = strtok(NULL,delim);
}
for(iter=1;iter<4;iter++){
darray[iter] = (int*)malloc(sizeof(char)*strlen(buf));
memcpy(&darray[iter],buf,(sizeof(char)*strlen(buf)));//
buf=strtok(NULL, delim);
}
}
record[idx].name=malloc(sizeof(char)*strlen(array[0]));
strcpy(record[idx].name,array[0]);
record[idx].literature=darray[1];
record[idx].math=darray[2];
record[idx].science=darray[3];
// printf("array value: %s %d %d %d\n",array[0], array[1], array[2], array[3]);
// printf("value after casting: %s %zu %zu %zu\n", array[0], (uintptr_t)array[1], (uintptr_t)array[2], (uintptr_t)array[3]);
//sscanf(buf,"%s %d %d %d",record[idx].name, &record[idx].literature, &record[idx].math, &record[idx].science);
printf("%s\n", line);
//fscanf(fptr,"%s %d %d %d",record[idx].name, &record[idx].literature, &record[idx].math, &record[idx].science);
line[strlen(line)-1]='\0';
printf("in idx loop %d %s %d %d %d\n\n", idx, record[idx].name, record[idx].literature, record[idx].math, record[idx].science);
idx++;
}
//output of file
printf("output commencing\n\n\n");
int tempave;
FILE *fout;
char *str1 = "Name Literature Math Science Ave.\n";
char *str2 = "Ave. ";
char *str3 = "Sarah\t 96\t 90\t 80\t 88.67";
char *str4 = "Minsu\t 55\t 70\t 76\t 67.00";
char *str5 = "Nara\t 88\t 70\t 96\t 84.67";
char *str6 = "79.67 76.67 84.00 80.11";
fout = fopen("output.txt", "w");
fprintf(fout,"%s\n", str1);
for(int idx=0;idx<i;idx++){
tempave = (record[idx].math+record[idx].literature+record[idx].science)/3;
fwrite(&record[idx],sizeof(struct studentT),1,fout);
fprintf(fout, "%d\n",tempave);
}
//fprintf(fout, "%s\n %s\n %s\n %s %s\n",str3,str4,str5,str2,str6);
fprintf(fout,"%s",str2);
float mathave,litave,sciave;
for(int idx=0;idx<3;idx++){
mathave+=record[idx].math;
litave+=record[idx].literature;
sciave+=record[idx].science;
}
mathave=mathave/3;
sciave=sciave/3;
litave=litave/3;
fprintf(fout,"%f %f %f",litave, mathave, sciave);
fclose(fptr);
fclose(fout);
}
I have wrote a new version of what you need with comments. Hopefully, you can understand what is happening and fix your code or you can use this as well. You may need to change the name of the inputfile and outputfile to work with your input file.
#include <stdio.h>
#include <stdlib.h>
struct student {
char *name;
int literature;
int math;
int science;
};
typedef struct student Student;
int main(void) {
FILE* inputFile;
FILE* outputFile;
int first_read; //check to see if the number of students value was read
int readNumStudents; //count of how many students are there
int i; //tracking students struct array
Student* students; //array of student structs
readNumStudents = 0;
first_read = 0;
i = 0;
inputFile = fopen("input.txt", "r"); //might have to change to whatever your file name is
outputFile = fopen("output.txt", "w"); //might have to change to whatever you want the output file name to be
//checking for null file pointers
if(inputFile == NULL){
printf("Failed to open input file.\n");
exit(1);
}
if(outputFile == NULL){
printf("Failed to open output file.\n");
exit(1);
}
//read file until end of file is reached
while(!feof(inputFile)){
if (first_read == 0){// check to see if first line was read. if not read, then write the value to readNumStudents variable
fscanf(inputFile, "%d", &readNumStudents);
first_read = 1; //set the value to 1. this means that the number of students was read.
students = (Student*) malloc(sizeof(Student) * readNumStudents);//allocate Student structs for the number of given students fro input file. (ie. 3 structs)
if(students == NULL){//check for null
printf("Failed to allocate memory for students.\n");
break;
}
//If not null, then for every struct allocate memory for the name string because its char* not char name[50]
for(int j = 0; j < readNumStudents; j++){
students[j].name = (char*) malloc(sizeof(char) * 50);
if(students[j].name == NULL){//check for null
printf("Failed to allocate memory for name.\n");
exit(1);
}
}
} else {//this means that the first line was read. now read the names and scors
//since you know the format of the input file (string int int int) you can use fscanf with "%s %d %d %d" and the adress of the struct values
fscanf(inputFile, "%s %d %d %d", students[i].name, &(students[i].literature), &(students[i].math), &(students[i].science));
i++;//used to point to next student structure
}
}
//Go thorugh every struct and write each value to outputfile
for(int j = 0; j < i; j++){
fprintf(outputFile, "%s %d %d %d\n", students[j].name, (students[j].literature), (students[j].math), (students[j].science));
}
//free all the allocated memory and files
fclose(inputFile);
fclose(outputFile);
//you need to free the char array on top of the studnets struct array. so loop through every student to free it
for(int j = 0; j < i; j++){
free(students[j].name);
}
free(students);
return 0;
}
I am writing a program to store information from an input file and print out information as selected by the user. I have not gotten to the use selection part yet but I am running into a segmentation fault right off the bat. I know that this means that I am trying to access places in memory that either don't exist or that I cannot access.
I am unsure of what I am doing wrong. I am trying to store the information from the input file into my structure.
The input file is in this format
3
5 Name Name 10 56789
7 Name Name 7 67894
8 Name Name 10 89375
I have tried to access the structures directly as emp[1].id etc instead of emp[i].id and such. This also did not work.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// structures
struct emp
{
int id;
char firstname[10];
char lastname[10];
int department;
float salary;
} emp[10];
// function prototypes
// nothing here yet
int main(int argc, char *argv[])
{
int i = 0;
int choice;
if(argc != 2){
printf("Usage: %s input.txt\n", argv[0]);
exit(EXIT_FAILURE);
}
FILE* inputFile;
inputFile = fopen("input.txt", "r");
if(inputFile == NULL){
printf("Error opening %s\n", argv[1]);
exit(EXIT_FAILURE);
}
// file is open now
// loop to save information from file into structure
int num;
fscanf(inputFile, "%d", &num);
for(i = 0; i < num; i++){
fscanf(inputFile, "%d", emp[i].id);
fscanf(inputFile, "%s", emp[i].firstname);
fscanf(inputFile, "%s", emp[i].lastname);
fscanf(inputFile, "%d", emp[i].department);
fscanf(inputFile, "%f", emp[i].salary);
}
printf("\n");
printf("Welcome to the Employee Database!\n");
printf("---------------------------------\n");
printf("Choose an option:\n");
printf("1: Print empid\n");
printf("2: Print ALL employees\n");
printf("3: Show ALL employees in department\n");
printf("-1: QUIT\n");
scanf("%d", &choice);
// I have not set up the functions to perform the selection options yet
return 0;
}
This is the output that I am receiving.
c803#cs2:~A5$ gcc A5.c
c803#cs2:~A5$ ./a.out input.txt
Segmentation fault
Here fscanf takes memory address of the variables to store the read data into, just like scanf().
You need to put '&' in front of emp[i].id and all other data memebers except character arrays as array name itself gives address of first array members of the array.
So the code should be::
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// structures
struct emp
{
int id;
char firstname[10];
char lastname[10];
int department;
float salary;
} emp[10];
// function prototypes
// nothing here yet
int main(int argc, char *argv[])
{
int i = 0;
int choice;
if(argc != 2){
printf("Usage: %s input.txt\n", argv[0]);
exit(EXIT_FAILURE);
}
FILE* inputFile;
inputFile = fopen("input.txt", "r");
if(inputFile == NULL){
printf("Error opening %s\n", argv[1]);
exit(EXIT_FAILURE);
}
// file is open now
// loop to save information from file into structure
int num;
fscanf(inputFile, "%d", &num);
for(i = 0; i < num; i++){
fscanf(inputFile, "%d", &emp[i].id);
fscanf(inputFile, "%s", emp[i].firstname);
fscanf(inputFile, "%s", emp[i].lastname);
fscanf(inputFile, "%d", &emp[i].department);
fscanf(inputFile, "%f", &emp[i].salary);
}
printf("\n");
printf("Welcome to the Employee Database!\n");
printf("---------------------------------\n");
printf("Choose an option:\n");
printf("1: Print empid\n");
printf("2: Print ALL employees\n");
printf("3: Show ALL employees in department\n");
printf("-1: QUIT\n");
scanf("%d", &choice);
// I have not set up the functions to perform the selection options yet
return 0;
}
I am creating a text file based on a binary file , reading the records from the binary file and writing them in the text file. The first subprogram is the creation of the binary file and the second for creating the text file.
#include<stdio.h>
typedef struct {
char CNP[14];
char nume[30];
int an;
int grupa;
int nrDisc;
int note[20];
}STUDENT;
void creare(char*nume) {
FILE*f;
STUDENT s;
fopen_s(&f, nume, "wb");
if (!f)
printf("Eroare");
else {
printf("CNP:");
gets(s.CNP);
while (!feof(stdin)){
printf("Nume:");
gets(s.nume);
printf("An:");
scanf_s("%d", &s.an);
printf("Grupa:");
scanf_s("%d", &s.grupa);
printf("Nr. discipline:");
scanf_s("%d", &s.nrDisc);
for (int i = 0; i < s.nrDisc; i++)
{
printf("Nota[%d] este:", i);
scanf_s("%d", &s.note[i]);
}
fwrite(&s, sizeof(s), 1, f);
getchar();
printf("CNP:");
gets(s.CNP);
}
fclose(f);
}
}
void raport(char*nume_binar, char*nume_text) {
FILE*f;
fopen_s(&f, nume_binar, "rb");
if (!f)
printf("Eroare");
else {
FILE*g;
STUDENT s;
fopen_s(&g, nume_text, "w");
while (fread(&s, sizeof(s), 1, f)==sizeof(s));
{
fprintf(g,"%s %s %d %d %d\n ", s.CNP, s.nume, s.an, s.grupa, s.nrDisc);
for (int i = 0; i < s.nrDisc; i++)
{
fprintf(g, "Nota %d este:", i);
fprintf(g, "%d\n", s.note[i]);
}
}
fclose(g);
}
fclose(f);
}
void main() {
char nume_fisier[] = "Student.dat";
char nume_fisier_txt[] = "Raport.txt";
//creare(nume_fisier);
raport(nume_fisier, nume_fisier_txt);
}
For example , I am writing 2 records in the binary file, but only the last record appears in the text file.
while (fread(&s, sizeof(s), 1, f)==sizeof(s));
You have an extra semicolon ';' here. It leads to while loop running continuously until it reaches end of file that is structure s contains last structure.
It means that
{
fprintf(g,"%s %s %d %d %d\n ", s.CNP, s.nume, s.an, s.grupa, s.nrDisc);
for (int i = 0; i < s.nrDisc; i++)
{
fprintf(g, "Nota %d este:", i);
fprintf(g, "%d\n", s.note[i]);
}
}
this block writing to text file runs only once and for last structure of binary file.
Also note that fread returns number of elements read not size of elements read so that statement should be while(fread(&s, sizeof(s), 1, f) == 1) {...}
I want to read strings and write them as full lines into a file, but I can't read more words into a buffer as a complete string.
Current problematic code:
printf("\nEnter how many sentences do you want to read: ");
scanf("%d", &n);
tab = (char**)malloc(n * sizeof(char*));
for (int i = 0; i < n; i++) {
printf("\nEnter sentence: ");
scanf("%s", val);
tab[i] = _strdup(val);
}
for (i = 0; i < n; i++)
fprintf(f, "%s ", tab[i]);
free(tab);
Previously I tried this: (problem is this only assigns one string)
printf("\nEnter how many sentences do you want to read: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter sentence: ");
scanf("%s", val);
fprintf(f, "\%s ", val);
}
Almost there, now i have sentences but i got one empty line as first line of file.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string>
#define SIZE 30
void creare(char t[30]);
void main(void)
{
FILE* f2;
char name[30];
printf("\nEnter name of file to work with: ");
scanf("%s", name);
creare(name);
f2 = fopen(name, "r");
if (f2 == NULL)
{
printf("\nOpen error!!");
exit(0);
}
fclose(f2);
printf("\n");
_getch();
}
void creare(char t[30])
{
FILE* f;
int n,i;
char val[30];
f = fopen(t, "w");
if (f == NULL)
{
printf("\nOpen error!!");
exit(0);
}
printf("\nEnter how many sentences do you want to read: ");
scanf("%d", &n);
for (i = 0; i <= n; i++)
{
fgets(val, sizeof(val), stdin);
fprintf(f, "% s", val);
}
fclose(f);
}
I have used fgets(val, sizeof val, stdin) to read the string, because to read string with spaces.
The reason why that blank line comes in file is due to the fact that you are reading "\n" that is inputted after scanf("%d", &n);The keystroke "\n" will be read into val for the first time so it simply prints that "\n" to the file.
In order to read that "\n" use a character to read that "\n". Below is the complete program.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define SIZE 30
void creare(char t[30]);
int main(void)
{
FILE* f2;
char name[30];
printf("\nEnter name of file to work with: ");
scanf("%s", name);
creare(name);
f2 = fopen(name, "r");
if (f2 == NULL)
{
printf("\nOpen error!!");
exit(0);
}
fclose(f2);
printf("\n");
}
void creare(char t[30])
{
FILE* f;
int n,i;
char val[30],g;
f = fopen(t, "w");
if (f == NULL)
{
printf("\nOpen error!!");
exit(0);
}
printf("\nEnter how many sentences do you want to read: ");
scanf("%d", &n);
scanf("%c",&g);
for (i = 0; i <n; i++)
{
fgets(val, sizeof(val), stdin);
fprintf(f, "%s", val);
}
fclose(f);
}