Need help reading information from file program in C
up vote
-1
down vote
favorite
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
char studentName[50];
int id;
};
struct student_detail
{
int day, month, year, grade;
struct student information;
}stu_data;
//---------------------------------//
int main()
{
struct student_detail stu_data[10];
int student_no, i=0, choice=0;
char keyword[50];
FILE *fptr;
printf("Add-Information(1) || Get-Information(2): ");
scanf("%d",&choice);
if(choice == 1){
fptr = (fopen("userInfo.txt","ab"));
system("CLS");
printf("How many students would you like to add?[MAX 10]: ");
scanf("%d",&student_no);
system("CLS");
for(i=0; i < student_no; i++){
system("CLS");
printf("Enter student#%d's name: ",i+1);
scanf("%s", stu_data[i].information.studentName);
printf("nWhat is %s's studentID?: ",stu_data[i].information.studentName);
scanf("%d",&stu_data[i].information.id);
printf("nWhat is %s's date of birth?(dd/mm/yy):n",stu_data[i].information.studentName);
scanf("%d %d %d",&stu_data[i].day, &stu_data[i].month, &stu_data[i].year);
fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].day, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].month, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].year, sizeof(struct student), 1, fptr);
}
fclose(fptr);
}
if(choice == 2){
fptr = (fopen("userInfo.txt","rb+"));
system("CLS");
printf("What students information would you like to retreive?: ");
scanf("%s",keyword);
fseek(fptr, sizeof(struct student), SEEK_SET);
fread(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fread(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fread(&stu_data[i].day, sizeof(struct student), 1, fptr);
fread(&stu_data[i].month, sizeof(struct student), 1, fptr);
fread(&stu_data[i].year, sizeof(struct student), 1, fptr);
printf("Name: %s",stu_data[i].information.studentName);
printf("nID: %d",stu_data[i].information.id);
printf("nDate of birth: %d/%d/%dnn",stu_data[i].day, stu_data[i].month, stu_data[i].year);
system("PAUSE");
fclose(fptr);
return 0;
}
}
This is my code, input into the file looks like this:Name: Riley
ID: 1
Date of birth: 01/10/2001
When I read the information from the file I get correct information but not all of it, when read in the message looks like this:Name: y
ID: 1
Date of birth: 10/2001/2686248
The writing works just not reading(sorry for lack of comments inside the program) Any information would be of great help! Thank you!
c arrays file
add a comment |
up vote
-1
down vote
favorite
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
char studentName[50];
int id;
};
struct student_detail
{
int day, month, year, grade;
struct student information;
}stu_data;
//---------------------------------//
int main()
{
struct student_detail stu_data[10];
int student_no, i=0, choice=0;
char keyword[50];
FILE *fptr;
printf("Add-Information(1) || Get-Information(2): ");
scanf("%d",&choice);
if(choice == 1){
fptr = (fopen("userInfo.txt","ab"));
system("CLS");
printf("How many students would you like to add?[MAX 10]: ");
scanf("%d",&student_no);
system("CLS");
for(i=0; i < student_no; i++){
system("CLS");
printf("Enter student#%d's name: ",i+1);
scanf("%s", stu_data[i].information.studentName);
printf("nWhat is %s's studentID?: ",stu_data[i].information.studentName);
scanf("%d",&stu_data[i].information.id);
printf("nWhat is %s's date of birth?(dd/mm/yy):n",stu_data[i].information.studentName);
scanf("%d %d %d",&stu_data[i].day, &stu_data[i].month, &stu_data[i].year);
fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].day, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].month, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].year, sizeof(struct student), 1, fptr);
}
fclose(fptr);
}
if(choice == 2){
fptr = (fopen("userInfo.txt","rb+"));
system("CLS");
printf("What students information would you like to retreive?: ");
scanf("%s",keyword);
fseek(fptr, sizeof(struct student), SEEK_SET);
fread(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fread(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fread(&stu_data[i].day, sizeof(struct student), 1, fptr);
fread(&stu_data[i].month, sizeof(struct student), 1, fptr);
fread(&stu_data[i].year, sizeof(struct student), 1, fptr);
printf("Name: %s",stu_data[i].information.studentName);
printf("nID: %d",stu_data[i].information.id);
printf("nDate of birth: %d/%d/%dnn",stu_data[i].day, stu_data[i].month, stu_data[i].year);
system("PAUSE");
fclose(fptr);
return 0;
}
}
This is my code, input into the file looks like this:Name: Riley
ID: 1
Date of birth: 01/10/2001
When I read the information from the file I get correct information but not all of it, when read in the message looks like this:Name: y
ID: 1
Date of birth: 10/2001/2686248
The writing works just not reading(sorry for lack of comments inside the program) Any information would be of great help! Thank you!
c arrays file
2
Please provide the return value for each use of scanf. Not the scanned value, the returned value, which indicates how many scans were successful.
– Yunnosch
Nov 21 at 16:37
3
Why is everyfread
sizesizeof(struct student)
?
– Osiris
Nov 21 at 16:38
2
fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);
would be the proper way to write one entry to the file. Same goes for read.
– Osiris
Nov 21 at 16:40
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
char studentName[50];
int id;
};
struct student_detail
{
int day, month, year, grade;
struct student information;
}stu_data;
//---------------------------------//
int main()
{
struct student_detail stu_data[10];
int student_no, i=0, choice=0;
char keyword[50];
FILE *fptr;
printf("Add-Information(1) || Get-Information(2): ");
scanf("%d",&choice);
if(choice == 1){
fptr = (fopen("userInfo.txt","ab"));
system("CLS");
printf("How many students would you like to add?[MAX 10]: ");
scanf("%d",&student_no);
system("CLS");
for(i=0; i < student_no; i++){
system("CLS");
printf("Enter student#%d's name: ",i+1);
scanf("%s", stu_data[i].information.studentName);
printf("nWhat is %s's studentID?: ",stu_data[i].information.studentName);
scanf("%d",&stu_data[i].information.id);
printf("nWhat is %s's date of birth?(dd/mm/yy):n",stu_data[i].information.studentName);
scanf("%d %d %d",&stu_data[i].day, &stu_data[i].month, &stu_data[i].year);
fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].day, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].month, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].year, sizeof(struct student), 1, fptr);
}
fclose(fptr);
}
if(choice == 2){
fptr = (fopen("userInfo.txt","rb+"));
system("CLS");
printf("What students information would you like to retreive?: ");
scanf("%s",keyword);
fseek(fptr, sizeof(struct student), SEEK_SET);
fread(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fread(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fread(&stu_data[i].day, sizeof(struct student), 1, fptr);
fread(&stu_data[i].month, sizeof(struct student), 1, fptr);
fread(&stu_data[i].year, sizeof(struct student), 1, fptr);
printf("Name: %s",stu_data[i].information.studentName);
printf("nID: %d",stu_data[i].information.id);
printf("nDate of birth: %d/%d/%dnn",stu_data[i].day, stu_data[i].month, stu_data[i].year);
system("PAUSE");
fclose(fptr);
return 0;
}
}
This is my code, input into the file looks like this:Name: Riley
ID: 1
Date of birth: 01/10/2001
When I read the information from the file I get correct information but not all of it, when read in the message looks like this:Name: y
ID: 1
Date of birth: 10/2001/2686248
The writing works just not reading(sorry for lack of comments inside the program) Any information would be of great help! Thank you!
c arrays file
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
char studentName[50];
int id;
};
struct student_detail
{
int day, month, year, grade;
struct student information;
}stu_data;
//---------------------------------//
int main()
{
struct student_detail stu_data[10];
int student_no, i=0, choice=0;
char keyword[50];
FILE *fptr;
printf("Add-Information(1) || Get-Information(2): ");
scanf("%d",&choice);
if(choice == 1){
fptr = (fopen("userInfo.txt","ab"));
system("CLS");
printf("How many students would you like to add?[MAX 10]: ");
scanf("%d",&student_no);
system("CLS");
for(i=0; i < student_no; i++){
system("CLS");
printf("Enter student#%d's name: ",i+1);
scanf("%s", stu_data[i].information.studentName);
printf("nWhat is %s's studentID?: ",stu_data[i].information.studentName);
scanf("%d",&stu_data[i].information.id);
printf("nWhat is %s's date of birth?(dd/mm/yy):n",stu_data[i].information.studentName);
scanf("%d %d %d",&stu_data[i].day, &stu_data[i].month, &stu_data[i].year);
fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].day, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].month, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].year, sizeof(struct student), 1, fptr);
}
fclose(fptr);
}
if(choice == 2){
fptr = (fopen("userInfo.txt","rb+"));
system("CLS");
printf("What students information would you like to retreive?: ");
scanf("%s",keyword);
fseek(fptr, sizeof(struct student), SEEK_SET);
fread(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fread(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fread(&stu_data[i].day, sizeof(struct student), 1, fptr);
fread(&stu_data[i].month, sizeof(struct student), 1, fptr);
fread(&stu_data[i].year, sizeof(struct student), 1, fptr);
printf("Name: %s",stu_data[i].information.studentName);
printf("nID: %d",stu_data[i].information.id);
printf("nDate of birth: %d/%d/%dnn",stu_data[i].day, stu_data[i].month, stu_data[i].year);
system("PAUSE");
fclose(fptr);
return 0;
}
}
This is my code, input into the file looks like this:Name: Riley
ID: 1
Date of birth: 01/10/2001
When I read the information from the file I get correct information but not all of it, when read in the message looks like this:Name: y
ID: 1
Date of birth: 10/2001/2686248
The writing works just not reading(sorry for lack of comments inside the program) Any information would be of great help! Thank you!
c arrays file
c arrays file
asked Nov 21 at 16:32
Krytec
124
124
2
Please provide the return value for each use of scanf. Not the scanned value, the returned value, which indicates how many scans were successful.
– Yunnosch
Nov 21 at 16:37
3
Why is everyfread
sizesizeof(struct student)
?
– Osiris
Nov 21 at 16:38
2
fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);
would be the proper way to write one entry to the file. Same goes for read.
– Osiris
Nov 21 at 16:40
add a comment |
2
Please provide the return value for each use of scanf. Not the scanned value, the returned value, which indicates how many scans were successful.
– Yunnosch
Nov 21 at 16:37
3
Why is everyfread
sizesizeof(struct student)
?
– Osiris
Nov 21 at 16:38
2
fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);
would be the proper way to write one entry to the file. Same goes for read.
– Osiris
Nov 21 at 16:40
2
2
Please provide the return value for each use of scanf. Not the scanned value, the returned value, which indicates how many scans were successful.
– Yunnosch
Nov 21 at 16:37
Please provide the return value for each use of scanf. Not the scanned value, the returned value, which indicates how many scans were successful.
– Yunnosch
Nov 21 at 16:37
3
3
Why is every
fread
size sizeof(struct student)
?– Osiris
Nov 21 at 16:38
Why is every
fread
size sizeof(struct student)
?– Osiris
Nov 21 at 16:38
2
2
fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);
would be the proper way to write one entry to the file. Same goes for read.– Osiris
Nov 21 at 16:40
fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);
would be the proper way to write one entry to the file. Same goes for read.– Osiris
Nov 21 at 16:40
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
I doubt you're writing or reading what you intend.
When you write,
fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
you're writing 1 element of sizeof(struct student)
bytes, starting not from the beginning of the student struct, but from stu_data[i].information.studentName
. So you get the 50-byte name, and whatever happens to follow it in memory. If sizeof(struct student)
is 250 bytes, you write 200 bytes of nonsense (at best), and then continue with the next field. Ugh!
Unless you have good reason not to, why not write the whole record at once? Less code, less bother for the computer.
fwrite(&stu_data[i], sizeof(struct student), 1, fptr);
As a rule, that's what you should look for in your code: the address provided as the 1st parameter should be of a data structure whose size is named in the 2nd. Even better:
fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);
The same advice applies to reading. I'll only point out one other error:
fseek(fptr, sizeof(struct student), SEEK_SET);
No matter what input is provided, you seek to (what you probably intend as) the 2nd record: sizeof(struct student)
bytes from the beginning of the file. Likely, you want some multiple of that number, based on the input.
Also, hexdump(1) is your friend, especially the -C option.
Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
– Krytec
Nov 26 at 16:07
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
I doubt you're writing or reading what you intend.
When you write,
fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
you're writing 1 element of sizeof(struct student)
bytes, starting not from the beginning of the student struct, but from stu_data[i].information.studentName
. So you get the 50-byte name, and whatever happens to follow it in memory. If sizeof(struct student)
is 250 bytes, you write 200 bytes of nonsense (at best), and then continue with the next field. Ugh!
Unless you have good reason not to, why not write the whole record at once? Less code, less bother for the computer.
fwrite(&stu_data[i], sizeof(struct student), 1, fptr);
As a rule, that's what you should look for in your code: the address provided as the 1st parameter should be of a data structure whose size is named in the 2nd. Even better:
fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);
The same advice applies to reading. I'll only point out one other error:
fseek(fptr, sizeof(struct student), SEEK_SET);
No matter what input is provided, you seek to (what you probably intend as) the 2nd record: sizeof(struct student)
bytes from the beginning of the file. Likely, you want some multiple of that number, based on the input.
Also, hexdump(1) is your friend, especially the -C option.
Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
– Krytec
Nov 26 at 16:07
add a comment |
up vote
0
down vote
accepted
I doubt you're writing or reading what you intend.
When you write,
fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
you're writing 1 element of sizeof(struct student)
bytes, starting not from the beginning of the student struct, but from stu_data[i].information.studentName
. So you get the 50-byte name, and whatever happens to follow it in memory. If sizeof(struct student)
is 250 bytes, you write 200 bytes of nonsense (at best), and then continue with the next field. Ugh!
Unless you have good reason not to, why not write the whole record at once? Less code, less bother for the computer.
fwrite(&stu_data[i], sizeof(struct student), 1, fptr);
As a rule, that's what you should look for in your code: the address provided as the 1st parameter should be of a data structure whose size is named in the 2nd. Even better:
fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);
The same advice applies to reading. I'll only point out one other error:
fseek(fptr, sizeof(struct student), SEEK_SET);
No matter what input is provided, you seek to (what you probably intend as) the 2nd record: sizeof(struct student)
bytes from the beginning of the file. Likely, you want some multiple of that number, based on the input.
Also, hexdump(1) is your friend, especially the -C option.
Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
– Krytec
Nov 26 at 16:07
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I doubt you're writing or reading what you intend.
When you write,
fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
you're writing 1 element of sizeof(struct student)
bytes, starting not from the beginning of the student struct, but from stu_data[i].information.studentName
. So you get the 50-byte name, and whatever happens to follow it in memory. If sizeof(struct student)
is 250 bytes, you write 200 bytes of nonsense (at best), and then continue with the next field. Ugh!
Unless you have good reason not to, why not write the whole record at once? Less code, less bother for the computer.
fwrite(&stu_data[i], sizeof(struct student), 1, fptr);
As a rule, that's what you should look for in your code: the address provided as the 1st parameter should be of a data structure whose size is named in the 2nd. Even better:
fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);
The same advice applies to reading. I'll only point out one other error:
fseek(fptr, sizeof(struct student), SEEK_SET);
No matter what input is provided, you seek to (what you probably intend as) the 2nd record: sizeof(struct student)
bytes from the beginning of the file. Likely, you want some multiple of that number, based on the input.
Also, hexdump(1) is your friend, especially the -C option.
I doubt you're writing or reading what you intend.
When you write,
fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
you're writing 1 element of sizeof(struct student)
bytes, starting not from the beginning of the student struct, but from stu_data[i].information.studentName
. So you get the 50-byte name, and whatever happens to follow it in memory. If sizeof(struct student)
is 250 bytes, you write 200 bytes of nonsense (at best), and then continue with the next field. Ugh!
Unless you have good reason not to, why not write the whole record at once? Less code, less bother for the computer.
fwrite(&stu_data[i], sizeof(struct student), 1, fptr);
As a rule, that's what you should look for in your code: the address provided as the 1st parameter should be of a data structure whose size is named in the 2nd. Even better:
fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);
The same advice applies to reading. I'll only point out one other error:
fseek(fptr, sizeof(struct student), SEEK_SET);
No matter what input is provided, you seek to (what you probably intend as) the 2nd record: sizeof(struct student)
bytes from the beginning of the file. Likely, you want some multiple of that number, based on the input.
Also, hexdump(1) is your friend, especially the -C option.
answered Nov 21 at 23:14
James K. Lowden
5,08411024
5,08411024
Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
– Krytec
Nov 26 at 16:07
add a comment |
Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
– Krytec
Nov 26 at 16:07
Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
– Krytec
Nov 26 at 16:07
Thank you! I only got around to seeing this now. The reading/writing tip saved my day.
– Krytec
Nov 26 at 16:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416607%2fneed-help-reading-information-from-file-program-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Please provide the return value for each use of scanf. Not the scanned value, the returned value, which indicates how many scans were successful.
– Yunnosch
Nov 21 at 16:37
3
Why is every
fread
sizesizeof(struct student)
?– Osiris
Nov 21 at 16:38
2
fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);
would be the proper way to write one entry to the file. Same goes for read.– Osiris
Nov 21 at 16:40