Saving top score in a file - c

So I want to save the best 3 scores of the game and to put them on a file. But for some reason when I read the file the best scores are 53,32,32. Not using name for now, just the 3 scores. And also I'm not familiarized with files.
typedef struct score{
unsigned char score[3];
//char name[20];
} SCORES;
This is how I'm saving.
void guardar_highscore (SCORES top){
FILE *f;
f = fopen ("/var/www/html/highscore3.txt","wb");
if (f ==NULL)
perror ("nope2"),exit (1);
fprintf(f,"%d \n %d \n %d \n",top.score[0],top.score[1],top.score[2]);
fclose(f);
}
This is how I'm reading it to the struct.
SCORES ler_highscore (){
SCORES top={0};
int i=0;
char line[20];
FILE *f;
f = fopen ("/var/www/html/highscore3.txt","rb");
if (f ==NULL)
perror ("nope"), exit (1);
while(fgets(line,20, f) != NULL){
sscanf (line, "%c", &top.score[i]);
i++;
}
fclose(f);
return top;
}

typedef struct score{
unsigned char score[3];
//char name[20];
} SCORES;
Scores are generally numbers, so it doesn't make much sense to store them as a single character. The problem becomes clear when you notice that you're writing them as integers (and with an extra space)...
fprintf(f,"%d \n %d \n %d \n",top.score[0],top.score[1],top.score[2]);
But you're reading them as characters...
sscanf (line, "%c", &top.score[i]);
53, 32, 32 looks suspiciously like the ASCII numbers for 5 and two spaces. If you write the character 5 as a number you'll get 53. That's because the character 5 is the number 53. Have a look at the ASCII table to see why.
The solution is to use integers consistently.
typedef struct {
int score[3];
} Scores;
Note that ALL_CAPS is generally reserved for constants, not types.
guardar_highscore remains basically the same, though I've cleaned it up some.
// The filename is now a variable so its used consistently
// and can be used in error messages.
const char Score_File[] = "highscore3.txt";
void guardar_highscore(const Scores *top) {
FILE *fd = fopen (Score_File,"wb");
if (fd == NULL) {
// A more informative error message than "nope".
fprintf( stderr, "Could not open '%s' for writing: %s\n", Score_File, strerror(errno) );
exit(1);
}
// Loop instead of repeating the formatting. This makes adding more
// scores easier.
// Note the stray whitespace is gone.
for( int i = 0; i < 3; i++ ) {
fprintf(fd, "%d\n", top->score[i]);
}
fclose(fd);
}
ler_highscore() changes to read as integers. It also only reads three lines to protect against overflowing the 3 element list if the file is unexpectedly large. It's good practice to never trust your input.
Scores ler_highscore() {
// This is the proper way to initialize a struct,
// each field must be initialized separately.
// A bare {0} happens to work because the struct is
// currently just a list, and if it doesn't you're
// going to overwrite all the elements anyway.
Scores top = { .score = {0} };
// No reason to skimp on the size of the line buffer.
char line[1024];
FILE *fd = fopen(Score_File, "rb");
if (fd == NULL) {
// Again, more informative error message.
fprintf( stderr, "Could not open '%s' for reading: %s", Score_File, strerror(errno) );
exit (1);
}
// Read 3 lines, no more. Otherwise we'll overflow memory.
for( int i = 0; i < 3; i++ ) {
// Use `sizeof(line)` rather than repeating the number.
// It avoids mistakenly letting them go out of sync.
if( fgets(line, sizeof(line), fd) == NULL ) {
fprintf( stderr, "Not enough scores in %s\n", Score_File );
break;
}
// Read one integer per line.
sscanf(line, "%d", &top.score[i]);
}
fclose(fd);
return top;
}

Related

Write struct into file in c

I am trying to write struct to file. I have done the part where I defined struct I read text from file and then I saved it in struct now I am on a point where I want to make change in struct: students[i].name and then I want the change to be reflected (write) in file but it has invalid encoding.
file.txt has this structure:
U,Virat Kohli,Virat Kohli,
U,Serena Williams,Virat Kohli,
G,Wayne Gretzky,Virat Kohli,
U,Virat Kohli,Virat Kohli,
U,Serena Williams,Virat Kohli,
G,Wayne Gretzky,Virat Kohli,
Here is my code:
#include <stdio.h>
#include <string.h>
typedef struct
{
// members for the student's type, name, surname
char type;
char name[50];
char surname[50];
} Student;
int main(void)
{
// for comparing strcmp
int result;
// file pointer variable for accessing the file
FILE *file;
// attempt to open file.txt in read mode to read the file contents
file = fopen("file.txt", "r");
// if the file failed to open, exit with an error message and status
if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
// array of structs for storing the Student data from the file
Student students[100];
// read will be used to ensure each line/record is read correctly
int read = 0;
// records will keep track of the number of Student records read from the file
int records = 0;
// read all records from the file and store them into the students array
while (fscanf(file, " %c , %49[^,], %49[^,],", &students[records].type, students[records].name, students[records].surname) == 3)
{
// if fscanf read 3 values from the file then we've successfully read
records++;
// if there was an error reading from the file exit with an error message
// and status
if (ferror(file))
{
printf("Error reading file.\n");
return 1;
}
}
// close the file as we are done working with it
fclose(file);
// print out the number of records read
printf("\n%d records read.\n\n", records);
// print out each of the records that was read
for (int i = 0; i < records; i++)
printf("%c %s %s\n",
students[i].type,
students[i].name,
students[i].surname);
printf("\n");
// change first record's name to Elena Heis
for (int i = 0; i < 1; i++)
{
if (students[i].name == students[i].name)
{
printf("%s\n",
students[i].name);
strcpy(students[i].name, "Elena Heis");
printf("%s\n",
students[i].name);
}
}
// write changes to file
file = fopen("file.txt", "wb");
if (file != NULL)
{
fwrite(students, sizeof(Student), 1, file);
fclose(file);
}
return 0;
}
After write file has broken encoding like this
It should be
Your code is clean and your format string is almost perfect, yet parsing the csv file (or any file in general) with fscanf() is not recommended as it is very difficult to recover from errors and newlines are mostly indistinguishable from other white space characters. In particular, the \n at the end of the format string will match any possibly empty sequence of white space characters.
Testing ferror() and feof() as you do seems fine but insufficient to ensure reliable parsing: if fscanf() returns a short code, for example because of an empty field, parsing will continue from the middle of the offending line and neither ferror() nor feof() will cause the loop to end.
You should instead read one line at a time with fgets() and use sscanf() to parse the line.
Also note these remarks:
the csv file does not seem to contain name and surname fields but rather the full names of opponents.
this file seems to have trailing , after the third field. If this is expected, the format string ensuring record validity should be changed to "%c,%49[^,],%49[^,\n]%1[,\n]"
you should check that records < 100 to avoid a buffer overflow.
the test if (students[i].name == students[i].name) is useless and always true. No test is needed to change the name of the first student.
you cannot write the text file with fwrite(students, sizeof(Student), 1, file), you should instead use fprintf as for the output to the terminal.
Here is a modified version:
#include <errno.h>
#include <stdio.h>
#include <string.h>
typedef struct
{
// members for the student's type, name, surname
char type;
char name[50];
char surname[50];
} Student;
int main(void)
{
// file pointer variable for accessing the file
FILE *file;
// attempt to open file.txt in read mode to read the file contents
file = fopen("file.txt", "r");
// if the file failed to open, exit with an error message and status
if (file == NULL)
{
fprintf(stderr, "Error opening file %s for reading: %s\n", "file.txt", strerror(errno));
return 1;
}
// array of structs for storing the Student data from the file
Student students[100];
// length of the students array
int nrecords = sizeof(students) / sizeof(*students);
// buffer to read one line at a time
char buffer[256];
// records will keep track of the number of Student records read from the file
int records = 0;
// read all records from the file and store them into the students array
while (records < nrecords && fgets(buffer, sizeof buffer, file))
{
// Read a line/record from the file
// if it was able to read successfully which we expect read will be 3
char newline[3]; // for characters at end of line: `,\n` or `\n`
char extra; // to ensure no more characters are present
// there are 5 conversion specifiers, but sscanf should parse
// exactly 4 fields, including the trailing `,` and the newline
int read = sscanf(buffer,
"%c,%49[^,],%49[^,\n]%2[,\n]%c",
&students[records].type,
students[records].name,
students[records].surname,
newline, &extra);
// if fscanf read 3 values and a newline from the file then we've successfully read
// in another record
if (read == 4) {
records++;
} else {
fprintf(stderr, "invalid record: %s", buffer);
}
}
// if there was an error reading from the file exit with an error message
// and status
if (ferror(file))
{
fprintf(stderr, "Error reading file: %s\n", strerror(errno));
fclose(file);
return 1;
}
// close the file as we are done working with it
fclose(file);
// print out the number of records read
printf("\n%d records read.\n\n", records);
// print out each of the records that was read
for (int i = 0; i < records; i++)
{
printf("%c %s %s\n",
students[i].type,
students[i].name,
students[i].surname);
}
printf("\n");
// change first record's name to Elena Heis
strcpy(students[0].name, "Elena Heis");
printf("%s\n", students[0].name);
// write changes to file
file = fopen("file.txt", "w");
if (file == NULL)
{
fprintf(stderr, "Error opening file %s for writing: %s\n", "file.txt", strerror(errno));
return 1;
}
// print out each of the records that was read
for (int i = 0; i < records; i++)
{
fprintf(file, "%c,%s,%s,\n",
students[i].type,
students[i].name,
students[i].surname);
}
fclose(file);
return 0;
}

How to read from a file and parse it

I have a file .txt containing some values formatted like this:
0,30,25,10
Now, I open up the file and store it into an array
char imposta_tratt[300];
FILE *fp;
fp = fopen("/home/pi/Documents/imposta_trattamento.txt", "r");
if (fp == 0) return;
fread(imposta_tratt, sizeof(imposta_tratt), 1, fp);
fclose(fp);
Now I expect to have the array filled with my data. I have the values separated by a , so I go on and parse it:
const char delim[2] = ",";
int t=0;
char *token = strtok(imposta_tratt, delim);
while (token!=NULL){
strcpy(tratt[t],token);
token = strtok(NULL, delim);
tratt[t]=token;
t++;
}
Here, referring to what's in the file .txt, I expect to have tratt[0]=0; tratt[1]=30; tratt[2]=25; and so on, but seems like I am missing something since it's not like this.
All I want is to have the values of the txt file stored in single variables. Can someone help?
What you are trying to achieve can simply be done using fgets():
bool read_file_content(const char *filename, const size_t tsizemax, int tratt[tsizemax], size_t *tsize, const char *delim)
{
// Attempt to open filename.
FILE *fp = fopen(filename, "r");
if (!fp) return false; // Return false upon failure.
// Try to read one line. If you have more, you need a while loop.
char imposta_tratt[300];
if (!fgets(imposta_tratt, sizeof imposta_tratt, fp)) {
fclose(fp);
return false;
}
*tsize = 0;
char tmp[300]; // Temporary buffer. Used for conversion into int.
char *token = strtok(imposta_tratt, delim);
while (token && *tsize < tsizemax) {
strncpy(tmp, token, sizeof tmp);
tratt[(*tsize)++] = atoi(tmp);
token = strtok(NULL, delim);
}
fclose(fp);
return true;
}
const char *filename: The file you want to parse.
const size_t tsizemax: The maximum size of your tratt array. It is important to control the size, otherwise your code will have buffer overflow (think of when your file has more than 100 tokens, for example).
int tratt[tsizemax]: The array that will hold the values.
size_t *tsize: The number of tokens read (used in combination of tsizemax).
const char *delim: The delimiter(s), in your case a ,.
This is your main():
int main(void)
{
int tratt[100];
size_t size = 0;
if (!read_file_content("in.txt", 100, tratt, &size, ",")) {
puts("Failed");
return 1;
}
for (size_t i = 0; i < size; ++i)
printf("%d\n", tratt[i]);
}
Output:
0
30
25
10
Suppose "in.txt" has contents
0,30,25,10
The below program uses fscanf to read the integers into the tratt array, one-by-one. As we read integers using fscanf, we make sure it's return value is as expected. If not, we close the file and exit. In the event that the return value of fscanf is not as expected, the program also prints which type of error occurred. Currently, if any error occurs, the program stops. However, you can make the program behave differently depending on the error that occurred if you like.
As output, the program prints all of the integers read into the tratt array. The output is
0
30
25
10
Now this program assumes we know the number of elements we want to read into tratt. If we do not, we could allow for dynamically allocating more memory should the array need more elements or perhaps "in.txt" could contain a data structure, say, at the beginning/end of the file that records information about the file, such as the number of numbers in the file and the data type (a binary file would be best suited for this). These are just a couple of the possibilities.
A better approach might be to read characters in one-by-one (say, using getc) and use strtol to convert a sequence of character digits to a long int (I would have taken an approach similar to this).
Nevertheless, this approach is more succinct and should suffice.
#include <stdio.h>
#include <stdlib.h>
#define FILE_NAME "in.txt"
#define MAX_LEN 4
int main(void) {
int i, tratt[MAX_LEN];
FILE *fp = fopen(FILE_NAME, "r"); /* open file for reading */
/* if cannot open file */
if (fp == NULL) {
printf("Cannot open %s\n", FILE_NAME);
exit(EXIT_FAILURE);
}
/* read integer, checking return value of scanf as expected */
if (fscanf(fp, "%d", &tratt[0]) != 1) {
if (ferror(fp))
printf("fscanf: read error\n");
else if (feof(fp))
printf("fscanf: end of file\n");
else
printf("fscanf: matching failure\n");
fclose(fp);
exit(EXIT_FAILURE);
}
for (i = 1; i < MAX_LEN; i++)
/* read comma plus integer, checking return value of scanf */
if (fscanf(fp, ",%d", &tratt[i]) != 1) {
if (ferror(fp))
printf("fscanf: read error\n");
else if (feof(fp))
printf("fscanf: end of file\n");
else
printf("fscanf: matching failure\n");
fclose(fp);
exit(EXIT_FAILURE);
}
fclose(fp); /* close file */
/* print integers stored in tratt */
for (i = 0; i < MAX_LEN; i++)
printf("%d\n", tratt[i]);
return 0;
}

Saving struct to a file in C

In my assignment I was given two functions to write
int openFileFromConsole(FILE *fp)
It takes an empty file pointer as input to the function using call by reference. And updates the pointer to point to the appropriate HDD address. IMPORTANT: Later, it asks the user through the console to provide the name and also asks for the file open type: "w", "r", "a" and UPDATES the file pointer. It returns 1 if succeeded, or 0 if fails.
void saveStudentGradeInfoTofile(FILE *fp)
This function gets the student info and saves it first into the student data-type (which is struct but disguised using typedef) then saves it to file. The student information is being provided by the register through the console. This takes the file pointer by reference as input and update the file using fprint.
This program is supposed to open a file through console depending on the user input then collect information and store them to a struct and save it to that file.I have written the two functions(correctly I think?).But I am having a hard time implementing them to main() since the first one is an int type function.Any help would be appreciated.
typedef struct student {
char name[50];
char id [20];
int score;
}student;
int openFileFromConsole(FILE *fp);
void saveStudentGradeInfoTofile(FILE *fp);
int main()
{
return 0;
}
int openFileFromConsole(FILE *fp){
char filePath[100],fileOpenType[10];
printf("Enter Path name: ");
scanf("%s", filePath);
printf("Enter file open type w, r or a: ");
scanf("%s", fileOpenType);
fp = fopen(filePath,fileOpenType);
if(fp != NULL) {
printf("File has been opened");
return 1;
}
else printf("File not found");
return 0;
}
void saveStudentGradeInfoTofile(FILE *fp){
int numOfStudent ;
student s[100];
printf ("Get the number of students :");
scanf("%d",&numOfStudent);
for(int i = 0; i < numOfStudent; i++) {
printf("\nEnter ID number:");
scanf(" %s",s[i].id);
printf("Enter name: ");
scanf("%s,",s[i].name);
printf("Enter score: ");
scanf("%d",&s[i].score);
fprintf(fp,"id : %s, Name: %s, score =%d\n",s[i].id,s[i].name,s[i].score);
printf("\n");
}
}
the posted code has a few problems.
it does not follow the axiom: only one statement per line and (at most) one variable declaration per statement. The result is the code is much more difficult to understand, debug, maintain.
The calls to system functions need to have their returned values checked to assure the operation was successful.
To modify where a pointer, located in the calling function, points, the pointer must be passed as a 'pointer to a pointer'.
when calling any of the scanf() family of functions, when using the '%s' input conversion specifier, always include a MAX CHARACTERS modifier that is 1 less than the length of the input buffer, so the user cannot overrun the input buffer. Such overrun results in undefined behavior and can lead to a seg fault event.
when using the '%s' input conversion specifier, insert a space before the specifier to consume any leading 'white space', like any newline sequence.
always cleanup when exiting a program. Do not leave the cleanup to the OS.
when working with numeric values that will never be less than 0, it is best to use size_t rather than int
The posted code is never reading from the student grade info file, so should never be opened with r
the 'mode' in a call to fopen() is always a string, even if it is only a single char, so needs to be written as "w" not just a w
the code contains lots of 'magic' numbers. 'magic' numbers are numbers with no basis. Such numbers make understanding, debugging, etc much more difficult than necessary. Suggest using an enum statement or #define statements to give those 'magic' numbers meaningful names, then use those meaningful names throughout the code.
for ease of readability and understanding, consistently indent the code. indent after every opening brace '{'. unindent after every closing brace '}'. Suggest using 4 spaces for each indent level as that is visible even with variable width fonts.
it is a poor programming practice to combine a 'struct' definition with a 'typedef'. code them separately.
when posting code, include the needed header file statements, so we do not have to guess what your code actually uses.
for ease of readability and understanding, separate code blocks (for, if, else, while, do...while, switch, case, default) via a single blank line. Separate functions by 2 or 3 blank lines (be consistent).
While modern compilers will handle the duplication of names, as modern compilers keep the struct names in a separate namespace from the typedef names, it is a poor programming practice that easily leads to confusion. Suggest keeping names unique (although that was not followed in this answer.
Here is a possible implementation of the desired functionality:
#include <stdio.h> // fopen(), fclose(), perror(), FILE
#include <stdlib.h> // exit(), EXIT_FAILURE
enum {
MAX_NAME_LEN =50,
MAX_ID_LEN =20,
MAX_PATH_LEN =100,
MAX_TYPE_LEN =10,
MAX_NUM_STUDENTS =100
};
struct student
{
char name[ MAX_NAME_LEN ];
char id [ MAX_ID_LEN ];
int score;
};
typedef struct student student;
int openFileFromConsole(FILE **fp);
void saveStudentGradeInfoTofile(FILE *fp);
int main( void )
{
FILE * fp = NULL;
int openStatus = openFileFromConsole(&fp);
if( 0 == openStatus )
{
saveStudentGradeInfoTofile( fp );
}
fclose( fp );
return 0;
} // end function: main
int openFileFromConsole(FILE **fp)
{
char filePath[ MAX_PATH_LEN ];
char fileOpenType;
printf("Enter Path name: ");
if( 1 != scanf("%100s", filePath) )
{
perror( "scanf for file path failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
printf("Enter file open type w, r or a: ");
if( 1 != scanf(" %c", &fileOpenType) )
{
perror( "scanf for file open type failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
char buffer[ MAX_TYPE_LEN ];
buffer[0] = '\"';
buffer[1] = fileOpenType;
buffer[2] = '\"';
buffer[3] = '\0';
*fp = fopen(filePath, buffer);
if( *fp )
{
return 1;
}
else
{
perror( "fopen failed" );
return 0;
}
} // end function: openFileFromConsole
void saveStudentGradeInfoTofile(FILE *fp)
{
size_t numOfStudent;
student s[ MAX_NUM_STUDENTS ];
printf ("Get the number of students :");
if( 1 != scanf("%lu",&numOfStudent) )
{
perror( "scanf for number of students failed:" );
fclose( fp );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
for( size_t i = 0; i < numOfStudent; i++)
{
printf("\nEnter ID number:");
if( 1 != scanf(" %19s",s[i].id) )
{
perror( "scanf for student ID failed" );
fclose( fp );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
printf("Enter name: ");
if( 1 != scanf(" %49s,",s[i].name) )
{
perror( "scanf for student ID failed" );
fclose( fp );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
printf("Enter score: ");
if( 1 != scanf("%d",&s[i].score) )
{
perror( "scanf for student Score failed" );
fclose( fp );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
fprintf(fp,"id : %s, Name: %s, score =%d\n",
s[i].id,
s[i].name,
s[i].score);
printf("\n");
} // end while
} // end function: saveStudentGradeInfoToFile
Just in case you want to pass the file path as an argument to your program
int main(int c /* argument counter */, char *v[] /*argument list */)
{
FILE *fp;
if(c >= 2) {
char *path = v[1]; /* this is the second argument (the first one, v[0], is the program's name): ./foo myfile.txt */
fp = fopen(path, "your mode goes here");
/* ... */
}
}

Read in text file with multiple elements per line

I need to read in a text file, "input.in", so that I can run a sort function on the code, according to id. The input.in file contains an id and name of file, 8 lines total. I know that I need to read in the input file line by line (not sure if my code is correct). But the main problem is that the fopen function is returning the result that it can't find the input file, even though its on the desktop along with the source file being saved there.
Any tips would be greatly appreciated
int main()
{
int id;
char node;
char item[9], status;
FILE *fp;
if((fp = fopen("/Users/jacobsprague/Desktop/input.txt", "r+")) == NULL)
{
printf("No such file\n");
exit(1);
}
while(42)
{
int ret = fscanf(fp, "%s %c", id, &node);
if(ret == 2)
printf("\n%s \t %c", id, node);
else if(errno != 0)
{
perror("scanf:");
break;
}
else if(ret == EOF)
{
break;
}
else
{
printf("No match.\n");
}
}
printf("\n");
if(feof(fp))
{
puts("EOF");
}
return 0;
}
Here is the input file contents:
8
4 Node1111
8 Node11111111
2 Node11
7 Node1111111
1 Node1
5 Node11111
6 Node111111
3 Node111
fopen can fail for reasons other than not finding the file, so you should check errno to see what the problem was. However in this case, as BLUEPIXY has mentioned, the problem appears to be that you have typed input.txt instead of input.in.
// 1) there were lots of little oops in the ops code,
// 2) the op skipped the detail that the first line contains
// a count of the number of following lines
// all of that is corrected in the following
#include <stdio.h>
#include <stdlib.h>
int main()
{
int id; // value read from file
char node[30]; // string read from file
//char item[9]; // if not commented, raises compiler warning about unused variable
//char status; // if not commented, raises compiler warning about unused variable
int ret; // returned value from fscanf
int lineCount = 0; // number of lines in file after first line
int i; // loop counter
FILE *fp;
if((fp = fopen("/Users/jacobsprague/Desktop/input.in", "r")) == NULL)
{
// perror also outputs the value of errno and the results of strerror()
perror( "fopen failed for file: input.in");
exit(1);
}
// implied else, fopen successful
// get first line, which contains count of following lines
if( 1 != (ret = fscanf(fp, " %d", &lineCount)) )
{ // fscanf failed
perror( "fscanf"); // this also outputs the value of errno and the results of strerror()
exit( EXIT_FAILURE );
}
// implied else, fscanf successful for lineCount
for( i=0; i < lineCount; i++) // read the data lines
{
// note leading space in format string to consume white space (like newline)
if( 2 != (ret = fscanf(fp, " %d %s", &id, node)) )
{ // fscanf failed
// this also outputs the value of errno and the results of strerror()
perror( "fscanf for id and node failed");
break;
}
// implied else, fscanf successful for id and node
printf("\n%d\t %s", id, node);
} // end for
printf("\n");
if( EOF == ret )
{
puts("EOF");
} // endif
return 0;
} // end function: main

fscanf, structs, and using the struct c programming

/*Project 1
Student records
1. Read the file with the records
2. store them
3. sort them
4. output them
ex. input and output (SORTED by student ID
2040003 AAAA BBBBBBBBB ComputerScience 3.45
2040002 AAA CCC ElectricalEngineering 3.01
2040005 AAAAAAAAAAAAAAAAA BBB ComputerScience 3.60
2040002,AAA,CCC,ElectricalEngineering,3.01
2040003,AAAA,BBBBBBBBB,ComputerScience,3.45
2040005,AAAAAAAAAAAAAAAAA,BBB,ComputerScience,3.60
char* name = malloc(256*sizeof(char));
*/
int main()
{
typedef struct StudentRecords
{
int StudentID; //must be of size 7 between 1000000 and 9999999
char *Firstname; //= MALLOC(256*sizeof(char)); // must be any length and allocate memory dynamically.
char *Lastname; //= MALLOC(256*sizeof(char));
char *Department; //= MALLOC(256*sizeof(char));
float GPA; // must be between 0 and 4
} STUDENTRECORDS;
/*
First job is read the file
*/
//set variables
int i=0;
char filecontent, file_name[100];
FILE *fp;
STUDENTRECORDS StudentRecords[300];
STUDENTRECORDS a[300];
int size =0;
printf("Enter directory of file\n"); // instructs user to enter directory of file
gets(file_name); //prompt use
fp = fopen(file_name,"r"); //opens the file "r" is read mode for fopen()
// here is a check to see if fp is empty and throw an error if so
if (fp == NULL)
{
perror("Could not open file\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name); // just prints the file name (file_name) you are prompted for
// here is where the printing of contents actually occurs
while ((filecontent = fgetc(fp)) != EOF) // I think EOF is end of feed here, not 100%
{
printf("%c",filecontent);
}
//I thought this line was to figure out how many lines are in the text, but it isnt working.
while (!feof(fp))
{
read(StudentRecords, i, fp);
i++;
}
//because the while statement isnt working, Ive elected to setting size to 3 in order to continue coding.
size = i = 3;
printf("Size = %d\n", size);
//I thought this essentially put the files contents into
for (i = 0; i < size; ++i)
fscanf(fp, "%d %s %s %s %f\n", &StudentRecords[i].StudentID, &StudentRecords[i].Firstname, &StudentRecords[i].Lastname, &StudentRecords[i].Department, &StudentRecords[i].GPA);
for (i = 0; i < size; ++i)
printf("%s", StudentRecords[i]);
//printf("%d %s %s %s %f/n", &StudentRecords[i].StudentID, &StudentRecords[i].Firstname, &StudentRecords[i].Lastname, &StudentRecords[i].Department, &StudentRecords[i].GPA);
for (i = 0; i < size; ++i)
fscanf(fp, "%d %s %s %s %f\n", &a[i].StudentID, &a[i].Firstname, &a[i].Lastname, &a[i].Department, &a[i].GPA);
for (i = 0; i < size; ++i)
printf("%s", a[i]);
//printf("%d %s %s %s %f/n", &a[i].StudentID, &a[i].Firstname, &a[i].Lastname, &a[i].Department, &a[i].GPA);
// fclose() must follow an fopen()
fclose(fp);
//printf("%g", &StudentRecords);
// return code
return 0;
}
How do I add information into a struct and print it or use it? This is what
i have so far. I've tried many different things and to no avail. I think the problem is with my initializing my struct for use. I can't get it right. I've tried searching for a solution, but each one is different and don't explain much.
Thanks for any suggestions.
Please find example code for reading content from file and storing it in structure also for this example have only take 5 student data entry(you can change as you wish) And on which criteria you want to do sorting? So i leave sorting on you.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ENTRY 5
typedef struct StudentRecords
{
int StudentID; //must be of size 7 between 1000000 and 9999999
char *Firstname; //= MALLOC(256*sizeof(char)); // must be any length and allocate memory dynamically.
char *Lastname; //= MALLOC(256*sizeof(char));
char *Department; //= MALLOC(256*sizeof(char));
float GPA; // must be between 0 and 4
} STUDENTRECORDS;
int main()
{
/*
First job is read the file
*/
//set variables
int i=0;
char filecontent, file_name[100];
FILE *fp;
STUDENTRECORDS StudentRecords[MAX_ENTRY];
for(i=0;i<MAX_ENTRY;i++)
{
StudentRecords[i].Firstname = malloc(sizeof(char)*256);
StudentRecords[i].Lastname = malloc(sizeof(char)*256);
StudentRecords[i].Department = malloc(sizeof(char)*256);
}
printf("Enter directory of file\n"); // instructs user to enter directory of file
gets(file_name); //prompt use
fp = fopen(file_name,"r"); //opens the file "r" is read mode for fopen()
// here is a check to see if fp is empty and throw an error if so
if (fp == NULL)
{
perror("Could not open file\n");
//exit(EXIT_FAILURE);
}
i=0;
while(EOF!=fscanf(fp, "%d %s %s %s %f\n", &StudentRecords[i].StudentID, StudentRecords[i].Firstname, StudentRecords[i].Lastname, StudentRecords[i].Department, &StudentRecords[i].GPA))
{
printf("%d %s %s %s %f\n", StudentRecords[i].StudentID, StudentRecords[i].Firstname, StudentRecords[i].Lastname, StudentRecords[i].Department, StudentRecords[i].GPA);
i++;
}
// fclose() must follow an fopen()
fclose(fp);
return 0;
}
Note: never forgot to free string which allocated by malloc after it's use.
A few comments, and some suggestions:
Was it your lecturer who said to use a linked-list? If so.. you really should do that otherwise you'll lose marks for failing to meet the spec.
EOF is 'End Of File'. feof() tells you if the stream pointer passed to it has hit EOF already.
Your while loop to print the contents is inefficient. Rather than reading every. single. character. in. the. file., you should read the entire file (or at least, large chunks thereof, let's not assume infinite memory), fclose() the stream and then operate on the read-in file.
Also, this sort of exercise lends itself very well to fixed-size record
Omitting error handling, variable declarations, structures and
using some pseudocode:
stat("/path/to/file", &statbuf)
inputfd = fopen("/path/to/file", r);
/* assuming that we only have a small file... */
contents = calloc(statbuf.st_size * sizeof(char));
/* read it all in, in one big chunk */
fread(contents, statbuf.st_size, 1, inputfd);
fclose(inputfd);
/* Now you can operate on it however you like */
bytesleft = statbuf.st_size;
/*
* this might need to go at the top of your block, depends on if you
* have enabled C99
*/
char eachline[MAXLINELENGTH];
int n = 0;
while (bytesleft > 0) {
add_new_list_element(mylist);
bzero(eachline, MAXLINELENTH);
memccpy(&eachline, contents[n], '\n', MAXLINELENGTH);
bytesleft -= sizeof(eachline);
nread = sscanf(start, "USE YOUR FORMAT STRING HERE", [variable list]);
if (nread < 0)
/* handle EOF, remember to make use of errno */
}
call_my_sort_function(mylist);
for (; thiselement != NULL; thiselement = thiselement->next)
print_salient_field_values(thiselement);

Resources