I want to write data in structure to a text file. But it ends up showing strange characters in the text file. I have changed many forms of fwrite arguments but none works. Please someone help me. Sorry for the long code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student
{
char name[30];
int id;
int score;
int score2;
int score3;
};
int main()
{
printf("Please input the information below into grade.data!\n");
printf("Ends with name's value equal to 'E'\n");
struct Student stu[10];
int i = 0, maxlength = 0; //size of longest name
printf("Name No Math Chi Eng\n");
while (true){
scanf("%s", stu[i].name);
if(maxlength < strlen(stu[i].name)) maxlength = strlen(stu[i].name);
if (stu[i].name[0] == 'E') break;
scanf("%d", &stu[i].id);
scanf("%d", &stu[i].score1);
scanf("%d", &stu[i].score2);
scanf("%d", &stu[i].score3);
i++;
}
FILE* fp;
fp = fopen("test.txt", "wb");
if (fp == NULL) {
printf("Open file error!");
exit(-1);
}
fwrite(&stu, sizeof(struct Student), 1, fp);
fclose(fp);
printf("Name%-*c No%-*c Math Chi Eng\n", maxlength-4, ' ', 7, ' ');
for (int i = 0; stu[i].name[0] != 'E'; i++) {
printf("%-*s ", maxlength, stu[i].name);
printf("%-*d ", 9, stu[i].id);
printf("%-*d ", 4, stu[i].score1);
printf("%-*d ", 3, stu[i].score2);
printf("%d\n", stu[i].score3);
}
return 0;
}
It because members in the structure Student other than name are integers. Convert integer values to ASCII and then store it in the file.
Related
I have written a small program to read in a series of strings from a file and then to store them in a 2D Arrray. The strings are read into the array correctly, yet my program is not countering the number of rows in the file like I had expected.
I am honestly at a loss and cannot figure out why the program is not counting the rows in the file. Any explanation as to what I am doing wrong is greatly appreciated.
Here is the code that I have so far:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE* fp;
char nameArray[20][120], str[20];
int i = 0, j = 0, n;
int count = 0;
char name[20]; //filename
int ch;
printf("Please enter a file name: ");
scanf("%s", &name);
fp = fopen(name, "r");
if (fp == NULL) {
printf("File \"%s\" does not exist!\n", name);
return -1;
}
while (fscanf(fp, "%s", str) != EOF)
{
strcpy(nameArray[i], str);
i++;
}
while ((ch = fgetc(fp)) != EOF) {
if (ch == '\n')
count++;
}
for (i = 0; i<=count; i++)
{
printf("%s", nameArray[i]);
}
fclose(fp);
return 0;
}
I have a .txt file following the syntax:
studentName1 grade1
studentName2 grade2
.
.
.
I want to develop the C function int changeGrade(String name, int old_grade, int new_grade, String fileName)
That changes the grade of the specified Student with the new one.
#include <stdio.h>
#include <string.h>
typedef char String[30];
void changeGrade(String name, int old_grade, int new_grade, String fileName) {
long pos;
String temp;
int grade;
int test = 0;
if ((old_grade <= 10) && (old_grade >= 0)) {
FILE * fp = fopen(fileName, "r+");
while ((!test) && (!feof(fp))) {
pos = ftell(fp);
fscanf(fp, "%s %d\n", temp, & grade);
if (strcmp(name, temp) == 0 && grade == old_grade) {
fseek(fp, pos, SEEK_SET);
fprintf(fp, "%s %d", name, new_grade);
test = 1;
}
}
fclose(fp);
} else printf("Error,you must specify the grade");
}
int main(int argc,
const char * argv[]) {
changeGrade("Carl", 7, 6, "students.txt");
}
The code works, but if i have something like
changeGrade("Carl",10,7,"students.txt") I get
Carl 70
Why?
Although I cannot condone doing this in C, nor the approach you are currently taking, the simple fix is for you to ensure that all your grades are the same width. If you must preprocess the file to make everything 2 digits, do that with something like: awk '{$2 = sprintf("%2d",$2)}1' input-file
Once that is done, just use a 2 digit grade all the time. That is, write:
fprintf(fp, "%s %2d", name, new_grade);
I am coding a song database. For now, the data of 2 songs are stored in a text file, one struct field per line. I would like to copy the content of the file line by line into an array, and I do not get why the program crashes after calling load(). Is it a problem related to fgets()? Or when I replace '\n' by '\0'?
Here are the interesting parts of the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "functions.h"
int main()
{
int menu;
bool exit = false;
/*char title[256];
char artist[256];
char album[256];*/
int year;
Song **songs = NULL; // base pointer to the array (of pointers to struct)
FILE *f; // pointer to a file structure
int n = 0; // height of array at the beginning (= number of pointers to struct)
int i;
f = fopen("database.txt", "r+");
if(f == NULL)
return 0;
count_songs_db(f, &n); // will modify n (height of array) according to the number of existing songs
printf("n = %d\n", n);
songs = (Song**)malloc(n*sizeof(Song));
load(f, songs, n);
// MENU
for(i = 0 ; i < n ; ++i)
free(songs[i]);
free(songs);
fclose(f);
return 0;
}
functions:
void count_songs_db(FILE *f, int *n) // calculate how many songs there are already in the database.
{
int c, n_lines = 0;
while ((c = getc(f)) != EOF)
if (c == '\n')
++n_lines; // count number of lines
*n = n_lines/6; // 1 song = 6 lines. Changes the height of array accordingly.
rewind(f); // go back to beginning of file, to be able to load the db
}
void load(FILE *f, Song **songs, int n) // load existing songs (in the file) into the array
{
int i;
for(i = 0 ; i < n ; ++i)
{
fgets(songs[i]->title, 256, f); // reads a line of text
songs[i]->title[strlen(songs[i]->title)-1] = '\0'; // to replace \n by \0 at the end // not working?
fgets(songs[i]->artist, 256, f);
songs[i]->title[strlen(songs[i]->artist)-1] = '\0';
fgets(songs[i]->album, 256, f);
songs[i]->title[strlen(songs[i]->album)-1] = '\0';
fscanf(f, "%d\n", &(songs[i]->year)); // use it like scanf
fgets(songs[i]->genre, 256, f);
songs[i]->title[strlen(songs[i]->genre)-1] = '\0';
fscanf(f, "%d:%d\n", &(songs[i]->length.m), &(songs[i]->length.s));
}
for(i = 0 ; i < n ; ++i)
{
printf("Title: %s\n", songs[i]->title);
printf("Artist: %s\n", songs[i]->artist);
printf("Album: %s\n", songs[i]->album);
printf("Year of release: %d\n", songs[i]->year);
printf("Genre: %s\n", songs[i]->genre);
printf("Length: %d:%d\n", songs[i]->length.m, songs[i]->length.s);
}
}
struct:
typedef struct Length {
int m, s;
} Length;
typedef struct Song {
char title[256];
char artist[256];
char album[256];
int year;
char genre[256];
Length length;
} Song;
Thanks for your help.
Edit: I modified the code to use a simple array of struct. Here is the add_song() function and save() function:
void add_song(Song *songs, int *n)
{
printf("Title: ");
read(songs[*n].title, MAX_SIZE); // another function is used instead of scanf(), so the user can enter string with spaces. Also more secure.
printf("Artist: ");
read(songs[*n].artist, MAX_SIZE);
printf("Album: ");
read(songs[*n].album, MAX_SIZE);
printf("Year of release: ");
songs[*n].year = read_long(); // still have to check the user inputs (ie. year has to be between 1900 and 2017)
printf("Genre: ");
read(songs[*n].genre, MAX_SIZE);
printf("Length: \nmin: ");
songs[*n].length.m = read_long();
printf("sec: ");
songs[*n].length.s = read_long();
++(*n);
}
void save(FILE *f, Song *songs, int n) // save song in file
{
fprintf(f, "%s\n%s\n%s\n%d\n%s\n%d:%d\n", songs[n-1].title, songs[n-1].artist, songs[n-1].album, songs[n-1].year, songs[n-1].genre, songs[n-1].length.m, songs[n-1].length.s); // use it like printf. Prints the data in the file.
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
typedef struct Length {
int m, s;
} Length;
typedef struct Song {
char title[256];
char artist[256];
char album[256];
int year;
char genre[256];
Length length;
} Song;
void count_songs_db(FILE *f, int *n) // calculate how many songs there are already in the database.
{
int c, n_lines = 0;
while ((c = getc(f)) != EOF)
if (c == '\n')
++n_lines; // count number of lines
*n = n_lines / 6; // 1 song = 6 lines. Changes the height of array accordingly.
rewind(f); // go back to beginning of file, to be able to load the db
}
void load(FILE *f, Song *songs, int n) // load existing songs (in the file) into the array
{
int i;
for (i = 0; i < n; ++i)
{
fgets(songs[i].title, 256, f); // reads a line of text
songs[i].title[strlen(songs[i].title) - 1] = '\0'; // to replace \n by \0 at the end // not working?
fgets(songs[i].artist, 256, f);
songs[i].title[strlen(songs[i].artist) - 1] = '\0';
fgets(songs[i].album, 256, f);
songs[i].title[strlen(songs[i].album) - 1] = '\0';
fscanf(f, "%d\n", &(songs[i].year)); // use it like scanf
fgets(songs[i].genre, 256, f);
songs[i].title[strlen(songs[i].genre) - 1] = '\0';
fscanf(f, "%d:%d\n", &(songs[i].length.m), &(songs[i].length.s));
}
for (i = 0; i < n; ++i)
{
printf("Title: %s\n", songs[i].title);
printf("Artist: %s\n", songs[i].artist);
printf("Album: %s\n", songs[i].album);
printf("Year of release: %d\n", songs[i].year);
printf("Genre: %s\n", songs[i].genre);
printf("Length: %d:%d\n", songs[i].length.m, songs[i].length.s);
}
}
int main()
{
int menu;
bool exit = false;
/*char title[256];
char artist[256];
char album[256];*/
int year;
Song *songs = NULL; // base pointer to the array (of pointers to struct)
FILE *f; // pointer to a file structure
int n = 0; // height of array at the beginning (= number of pointers to struct)
int i;
f = fopen("database.txt", "r+");
if (f == NULL)
return 0;
count_songs_db(f, &n); // will modify n (height of array) according to the number of existing songs
printf("n = %d\n", n);
songs = (Song*)malloc(n * sizeof(Song));
load(f, songs, n);
// MENU
free(songs);
fclose(f);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SLENG 50 //just a random value
typedef struct Song
{
char *name;
char *nameSong;
char *timeSong;
int date;
} Song;
void saveToFile(Song *x, int *songCount) //Saves info to the binary file
{
FILE *f = fopen("array.txt", "w");
if (f == NULL)
{
printf("Error\n");
}
fwrite(songCount, sizeof(int), 1, f);
fwrite(x, sizeof(struct Song), (*songCount), f);
fclose(f);
}
void readSong(Song *x, int *songCount) //Reads info fromt he file and writes it
{
FILE *fr = fopen("array.txt", "r");
if (fr == NULL)
{
printf("Error\n");
}
printf("Songs:\n");
fread(songCount, sizeof(int), 1, fr);
fread(x, sizeof(struct Song), (*songCount), fr);
for(int i=0; i < (*songCount); i++)
{
printf("%d. %s %s %s %d\n", (i+1), x[i].name, x[i].nameSong, x[i].timeSong, x[i].date);
}
fclose(fr);
}
void insertSong(Song *x, int Count) //Inserts new song into the array.
{
printf("\nInsert name of the band:\n");
x[Count].name=malloc(SLENG * sizeof(char));
scanf("%s", x[Count].name);
printf("Insert name of the song:\n");
x[Count].nameSong=malloc(SLENG * sizeof(char));
scanf("%s", x[Count].nameSong);
printf("Insert length of the song:\n");
x[Count].timeSong=malloc(SLENG * sizeof(char));
scanf("%s", x[Count].timeSong);
printf("Insert then song was created:\n");
scanf("%d", &(x[Count].date));
printf("\n");
}
main()
{
int songCount, menuOption;
Song *x=malloc(SLENG*sizeof(char)+SLENG*sizeof(char)+SLENG*sizeof(char)+sizeof(int));
printf("1. insert song\n 2. load from file\n ");
scanf("%d", &menuOption);
switch(menuOption)
{
case(1) :
printf("Insert how many songs do you want to input?\n");
scanf("%d", &songCount);
for(int i=0; i<songCount; i++)
{
insertSong(x, i);
}
saveToFile(x, &songCount);
break;
case(2) :
readSong(x, &songCount);
break;
}
}
I have an assingment to write a programm which would input some data into file and could read that data from that file, the problem is probably with fwrite or fread, couse it seems to crash everytime I try to load the and write the data from file. Any ideas why is it not working properly? And can I even do it like this as it is dynamic struct array. Thanks in advance.
In order to save the structure to a file, it must only contain scalar values, not pointers into memory objects. Modify your structure to use arrays instead of pointers:
typedef struct Song {
char name[SLENG];
char nameSong[SLENG];
char timeSong[SLENG];
int date;
} Song;
And modify the code accordingly, but note that:
saving and reading the structures to and from a file requires opening it in binary mode "wb" and "rb".
it is very misleading to name a binary file array.txt.
you do not need to pass the address of the count when writing to the file, but you need to pass the address of the array pointer when reading as you do not know yet how much memory to allocate.
Here is the modified code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SLENG 50 // this value is used in the file format
typedef struct Song {
char name[SLENG];
char nameSong[SLENG];
char timeSong[SLENG];
int date;
} Song;
int saveToFile(Song *x, int songCount) { //Saves info to the binary file
FILE *f = fopen("array.bin", "wb");
if (f == NULL) {
printf("Error\n");
return -1;
}
fwrite(songCount, sizeof(int), 1, f);
int written = fwrite(x, sizeof(struct Song), songCount, f);
fclose(f);
return written;
}
int readSong(Song **x, int *songCount) { //Reads info from the file and writes it
int count = 0;
FILE *fr = fopen("array.bin", "rb");
if (fr == NULL) {
printf("Error\n");
return -1;
}
printf("Songs:\n");
fread(&count, sizeof(int), 1, fr);
*x = calloc(count, sizeof(Song));
if (*x == NULL) {
printf("Cannot allocate %d bytes of memory\n", count);
fclose(fr);
return -1;
}
int found = fread(*x, sizeof(struct Song), count, fr);
for (int i = 0; i < found; i++) {
printf("%d. %s %s %s %d\n", i + 1,
(*x)[i].name, (*x)[i].nameSong, (*x)[i].timeSong, (*x)[i].date);
}
fclose(fr);
return *songCount = found;
}
void insertSong(Song *x, int Count) { //Inserts new song into the array.
printf("\nInsert name of the band:\n");
scanf("%49s", x[Count].name);
printf("Insert name of the song:\n");
scanf("%49s", x[Count].nameSong);
printf("Insert length of the song:\n");
scanf("%49s", x[Count].timeSong);
printf("Insert then song was created:\n");
scanf("%d", &(x[Count].date));
printf("\n");
}
int main(void) {
int songCount, menuOption;
Song *x = NULL;
printf("1. insert song\n 2. load from file\n ");
scanf("%d", &menuOption);
switch (menuOption) {
case 1:
printf("Insert how many songs do you want to input?\n");
if (scanf("%d", &songCount) == 1) {
x = calloc(songCount, sizeof(Song));
for (int i = 0; i < songCount; i++) {
insertSong(x, i);
}
saveToFile(x, songCount);
}
break;
case 2:
readSong(&x, &songCount);
break;
}
free(x);
x = NULL;
return 0;
}
I'm trying to store the .txt file into struct, not being successful.
I dont need the first line stored, but I do need the rest.
Thanks guys !! =)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
struct candidates
{
char name[25];
char gender[5];
int height;
int weight;
};
struct candidates values[25];
FILE *fp;
if ((fp=fopen("candidatesdata.txt","r"))==NULL){printf("Unable to open file\n");}
int x = 0;
int iterations = 0;
while((fscanf(fp,"%s,%s,%d,%d",&values[x].name, &values[x].gender, &values[x].height, &values[x].weight))!=EOF)
{
x++;
iterations +=1;
}
fclose(fp);
//values[15].weight = 300;
printf("\n%d\t%d",iterations,values[1].height);
return 0;
}
Text file looks like the following:
Name,Gender,Height,Weight
Tanner,M,71.8,180.25
John,M,70.75,185.3
Parker,F,65.25,120.3
Meeks,M,57.25,210.2
struct candidates
{
char name[25];
char gender[5]; // can be char gender too !!
int height; // should be changed to float height
int weight; // should be changed to float height
};
If you actually have :
Name,Gender,Height,Weight
in your file, you might do his before you go to the while loop :
char somebigstring[100];
.
.
.
if(fscanf(fp,"%s",somebigstring) == EOF) //wasting the first line
exit(-1); // exiting here if there is no first line
fix like this:
#include <stdio.h>
#include <stdlib.h>
struct candidates{
char name[25];
char gender[7];//Female+1
float height;
float weight;
};
int main(void) {
struct candidates values[25];
FILE *fp;
if ((fp=fopen("candidatesdata.txt","r"))==NULL){
fprintf(stderr, "Unable to open file\n");
exit(EXIT_FAILURE);
}
char line[64];
int x = 0;
int iterations = 0;
while(fgets(line, sizeof line, fp)){
iterations += 1;
if(x < 25 && sscanf(line, "%24[^,],%6[^,],%f,%f", values[x].name, values[x].gender, &values[x].height, &values[x].weight)==4){
x++;
} else if(iterations != 1){
fprintf(stderr, "invalid data in %d line: %s", iterations, line);
}
}
fclose(fp);
for(int i = 0; i < x; ++i){
printf("%s,%c,%.2f,%.2f\n", values[i].name, *values[i].gender, values[i].height, values[i].weight);
}
return 0;
}
This code does two following things; first is writes data into .txt file and the second it reads data from .txt file and stores in struct data type which is what you are trying achieve but only for single entity. Next you should implement this as for entities.
#include<stdio.h>
#include<stdlib.h>
typedef struct{
char name[30];
//other fields
}Candidate;
typedef struct{
Candidate c;
char city[30];
int vote;
}CityVotes;
//for .bin operations
void write_to_file(FILE *f)
{
Candidate c;
CityVotes cv;
printf("Enter candidate name : ");
scanf("%s",&c.name);
cv.c = c;
printf("Enter the city : ");
scanf("%s",&cv.city);
printf("Enter the vote : ");
scanf("%d",&cv.vote);
fwrite(&cv, sizeof(cv), 1, f);
}
//for .txt operations
void write_to_file1(FILE *f)
{
Candidate c;
CityVotes cv;
printf("Enter candidate name : ");
scanf("%s",c.name);
cv.c = c;
printf("Enter the city : ");
scanf("%s",&cv.city);
printf("Enter the vote : ");
scanf("%d",&cv.vote);
fprintf(f,"%s ", cv.c.name);
fprintf(f,"%s ", cv.city);
fprintf(f,"%d ", cv.vote);
}
//for .bin operations
void read_file(FILE *f)
{
CityVotes cv;
while(fread(&cv, sizeof(cv), 1, f)){
printf("Candidate : %s\t",cv.c.name);
printf("City.: %s\t",cv.city);
printf("Vote.: %d\n",cv.vote);
}
}
//for .txt operations
void read_file1(FILE *f){
CityVotes cv;
fscanf(f," %s", &cv.c.name);
fscanf(f," %s", &cv.city);
fscanf(f," %d", &cv.vote);
printf("Candidate : %s\t",cv.c.name);
printf("City.: %s\t",cv.city);
printf("Vote.: %d\n",cv.vote);
}
int main(void)
{
FILE *f;
f=fopen("candidates.txt","w");
write_to_file1(f);
fclose(f);
f=fopen("candidates.txt","r");
read_file1(f);
return 0;
}
Be aware, the code just is example , you should check for nulls.