Write to a file, where the output is tabbed instead of spaced
up vote
0
down vote
favorite
I'm working on a program that records students grades. The user inputs first name, last name, student ID, grade, and assignment name. I'm supposed to have the input from the user separated by being tabbed rather than a space. But when I execute the program the output in the txt file isn't correct.
#include <stdio.h>
int main(void){
FILE *cfPtr; // cfPtr = clients.txt file pointer
// fopen opens file. Exit program if unable to create file
if ((cfPtr = fopen("grades.txt", "w")) == NULL) {
puts("File could not be opened.");
}
else {
puts("Enter the students first & last name, ID, grade, and assignment.");
puts("Enter EOF to end input.");
printf("%s", "? ");
char firstName[30]; // student first name
char lastName[30]; // student last name
unsigned int stuID; // student ID
double grade; // student grade
char assignment[10]; // student assignment name
scanf("%5s%5s%d%lf%5s", firstName, lastName, &stuID, &grade, assignment);
// write first & last name, student ID, grade, and assignment name into file with fprintf
while(!feof(stdin)) {
fprintf(cfPtr, "%st%st%dt%.2ft%stn", firstName, lastName, stuID, grade, assignment);
fflush(cfPtr);
printf("%s", "? ");
scanf("%5s%5s%d%lf%5s", firstName, lastName, &stuID, &grade, assignment);
} // end while
fclose(cfPtr); // fclose closes file
} // end else
} // end main
Heres what happens when I execute it:
Enter the students first & last name, ID, grade, and assignment.
Enter EOF to end input.
? Freddy Krueger 1234 22.33 test1
? ? Jason Vorhees 1235 33.00 test1
? ? The Hulk 1236 2.95 test1
? Bat Man 1237 100.00 test1
? ^Z
[1]+ Stopped ./seqw
grade.txt file output:
Fredd y 0 0.00 ttime
Krueg er 1234 22.33 test1
Jason Vorhe 1234 22.33 test1
es 1235 33 0.00 test1
The Hulk 1236 2.95 test1
Bat Man 1237 100.00 test1
What am I doing wrong here? And where did ttime come from? And it's strange how it's tabbing part of the last name and making it another input.
c
|
show 2 more comments
up vote
0
down vote
favorite
I'm working on a program that records students grades. The user inputs first name, last name, student ID, grade, and assignment name. I'm supposed to have the input from the user separated by being tabbed rather than a space. But when I execute the program the output in the txt file isn't correct.
#include <stdio.h>
int main(void){
FILE *cfPtr; // cfPtr = clients.txt file pointer
// fopen opens file. Exit program if unable to create file
if ((cfPtr = fopen("grades.txt", "w")) == NULL) {
puts("File could not be opened.");
}
else {
puts("Enter the students first & last name, ID, grade, and assignment.");
puts("Enter EOF to end input.");
printf("%s", "? ");
char firstName[30]; // student first name
char lastName[30]; // student last name
unsigned int stuID; // student ID
double grade; // student grade
char assignment[10]; // student assignment name
scanf("%5s%5s%d%lf%5s", firstName, lastName, &stuID, &grade, assignment);
// write first & last name, student ID, grade, and assignment name into file with fprintf
while(!feof(stdin)) {
fprintf(cfPtr, "%st%st%dt%.2ft%stn", firstName, lastName, stuID, grade, assignment);
fflush(cfPtr);
printf("%s", "? ");
scanf("%5s%5s%d%lf%5s", firstName, lastName, &stuID, &grade, assignment);
} // end while
fclose(cfPtr); // fclose closes file
} // end else
} // end main
Heres what happens when I execute it:
Enter the students first & last name, ID, grade, and assignment.
Enter EOF to end input.
? Freddy Krueger 1234 22.33 test1
? ? Jason Vorhees 1235 33.00 test1
? ? The Hulk 1236 2.95 test1
? Bat Man 1237 100.00 test1
? ^Z
[1]+ Stopped ./seqw
grade.txt file output:
Fredd y 0 0.00 ttime
Krueg er 1234 22.33 test1
Jason Vorhe 1234 22.33 test1
es 1235 33 0.00 test1
The Hulk 1236 2.95 test1
Bat Man 1237 100.00 test1
What am I doing wrong here? And where did ttime come from? And it's strange how it's tabbing part of the last name and making it another input.
c
I would start by checking whatscanf
is returning. If you didn't scan the right number of items you should stop there. You should also test your input for when yo stop, not after the fact with!feof
. stackoverflow.com/questions/5431941/…
– Retired Ninja
Nov 19 at 23:01
3
Why are you restricting the string inputs to 5 characters? What do you think happens to the rest of the truncated strings?
– Weather Vane
Nov 19 at 23:06
1
You have 30-character variables, but only scan 5 characters
– stark
Nov 19 at 23:11
2
Please see Why iswhile ( !feof (file) )
always wrong?. You should always check the return value fromscanf
function family, and an idiomatic way to deal with both problems iswhile (scanf(...) == 5) { ... }
– Weather Vane
Nov 19 at 23:11
Your scanf statement should look likescanf("%30s%30s%u%d%10s", ...)
.
– eapetcho
Nov 20 at 4:13
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm working on a program that records students grades. The user inputs first name, last name, student ID, grade, and assignment name. I'm supposed to have the input from the user separated by being tabbed rather than a space. But when I execute the program the output in the txt file isn't correct.
#include <stdio.h>
int main(void){
FILE *cfPtr; // cfPtr = clients.txt file pointer
// fopen opens file. Exit program if unable to create file
if ((cfPtr = fopen("grades.txt", "w")) == NULL) {
puts("File could not be opened.");
}
else {
puts("Enter the students first & last name, ID, grade, and assignment.");
puts("Enter EOF to end input.");
printf("%s", "? ");
char firstName[30]; // student first name
char lastName[30]; // student last name
unsigned int stuID; // student ID
double grade; // student grade
char assignment[10]; // student assignment name
scanf("%5s%5s%d%lf%5s", firstName, lastName, &stuID, &grade, assignment);
// write first & last name, student ID, grade, and assignment name into file with fprintf
while(!feof(stdin)) {
fprintf(cfPtr, "%st%st%dt%.2ft%stn", firstName, lastName, stuID, grade, assignment);
fflush(cfPtr);
printf("%s", "? ");
scanf("%5s%5s%d%lf%5s", firstName, lastName, &stuID, &grade, assignment);
} // end while
fclose(cfPtr); // fclose closes file
} // end else
} // end main
Heres what happens when I execute it:
Enter the students first & last name, ID, grade, and assignment.
Enter EOF to end input.
? Freddy Krueger 1234 22.33 test1
? ? Jason Vorhees 1235 33.00 test1
? ? The Hulk 1236 2.95 test1
? Bat Man 1237 100.00 test1
? ^Z
[1]+ Stopped ./seqw
grade.txt file output:
Fredd y 0 0.00 ttime
Krueg er 1234 22.33 test1
Jason Vorhe 1234 22.33 test1
es 1235 33 0.00 test1
The Hulk 1236 2.95 test1
Bat Man 1237 100.00 test1
What am I doing wrong here? And where did ttime come from? And it's strange how it's tabbing part of the last name and making it another input.
c
I'm working on a program that records students grades. The user inputs first name, last name, student ID, grade, and assignment name. I'm supposed to have the input from the user separated by being tabbed rather than a space. But when I execute the program the output in the txt file isn't correct.
#include <stdio.h>
int main(void){
FILE *cfPtr; // cfPtr = clients.txt file pointer
// fopen opens file. Exit program if unable to create file
if ((cfPtr = fopen("grades.txt", "w")) == NULL) {
puts("File could not be opened.");
}
else {
puts("Enter the students first & last name, ID, grade, and assignment.");
puts("Enter EOF to end input.");
printf("%s", "? ");
char firstName[30]; // student first name
char lastName[30]; // student last name
unsigned int stuID; // student ID
double grade; // student grade
char assignment[10]; // student assignment name
scanf("%5s%5s%d%lf%5s", firstName, lastName, &stuID, &grade, assignment);
// write first & last name, student ID, grade, and assignment name into file with fprintf
while(!feof(stdin)) {
fprintf(cfPtr, "%st%st%dt%.2ft%stn", firstName, lastName, stuID, grade, assignment);
fflush(cfPtr);
printf("%s", "? ");
scanf("%5s%5s%d%lf%5s", firstName, lastName, &stuID, &grade, assignment);
} // end while
fclose(cfPtr); // fclose closes file
} // end else
} // end main
Heres what happens when I execute it:
Enter the students first & last name, ID, grade, and assignment.
Enter EOF to end input.
? Freddy Krueger 1234 22.33 test1
? ? Jason Vorhees 1235 33.00 test1
? ? The Hulk 1236 2.95 test1
? Bat Man 1237 100.00 test1
? ^Z
[1]+ Stopped ./seqw
grade.txt file output:
Fredd y 0 0.00 ttime
Krueg er 1234 22.33 test1
Jason Vorhe 1234 22.33 test1
es 1235 33 0.00 test1
The Hulk 1236 2.95 test1
Bat Man 1237 100.00 test1
What am I doing wrong here? And where did ttime come from? And it's strange how it's tabbing part of the last name and making it another input.
c
c
asked Nov 19 at 22:58
SgtCheespuffs
133
133
I would start by checking whatscanf
is returning. If you didn't scan the right number of items you should stop there. You should also test your input for when yo stop, not after the fact with!feof
. stackoverflow.com/questions/5431941/…
– Retired Ninja
Nov 19 at 23:01
3
Why are you restricting the string inputs to 5 characters? What do you think happens to the rest of the truncated strings?
– Weather Vane
Nov 19 at 23:06
1
You have 30-character variables, but only scan 5 characters
– stark
Nov 19 at 23:11
2
Please see Why iswhile ( !feof (file) )
always wrong?. You should always check the return value fromscanf
function family, and an idiomatic way to deal with both problems iswhile (scanf(...) == 5) { ... }
– Weather Vane
Nov 19 at 23:11
Your scanf statement should look likescanf("%30s%30s%u%d%10s", ...)
.
– eapetcho
Nov 20 at 4:13
|
show 2 more comments
I would start by checking whatscanf
is returning. If you didn't scan the right number of items you should stop there. You should also test your input for when yo stop, not after the fact with!feof
. stackoverflow.com/questions/5431941/…
– Retired Ninja
Nov 19 at 23:01
3
Why are you restricting the string inputs to 5 characters? What do you think happens to the rest of the truncated strings?
– Weather Vane
Nov 19 at 23:06
1
You have 30-character variables, but only scan 5 characters
– stark
Nov 19 at 23:11
2
Please see Why iswhile ( !feof (file) )
always wrong?. You should always check the return value fromscanf
function family, and an idiomatic way to deal with both problems iswhile (scanf(...) == 5) { ... }
– Weather Vane
Nov 19 at 23:11
Your scanf statement should look likescanf("%30s%30s%u%d%10s", ...)
.
– eapetcho
Nov 20 at 4:13
I would start by checking what
scanf
is returning. If you didn't scan the right number of items you should stop there. You should also test your input for when yo stop, not after the fact with !feof
. stackoverflow.com/questions/5431941/…– Retired Ninja
Nov 19 at 23:01
I would start by checking what
scanf
is returning. If you didn't scan the right number of items you should stop there. You should also test your input for when yo stop, not after the fact with !feof
. stackoverflow.com/questions/5431941/…– Retired Ninja
Nov 19 at 23:01
3
3
Why are you restricting the string inputs to 5 characters? What do you think happens to the rest of the truncated strings?
– Weather Vane
Nov 19 at 23:06
Why are you restricting the string inputs to 5 characters? What do you think happens to the rest of the truncated strings?
– Weather Vane
Nov 19 at 23:06
1
1
You have 30-character variables, but only scan 5 characters
– stark
Nov 19 at 23:11
You have 30-character variables, but only scan 5 characters
– stark
Nov 19 at 23:11
2
2
Please see Why is
while ( !feof (file) )
always wrong?. You should always check the return value from scanf
function family, and an idiomatic way to deal with both problems is while (scanf(...) == 5) { ... }
– Weather Vane
Nov 19 at 23:11
Please see Why is
while ( !feof (file) )
always wrong?. You should always check the return value from scanf
function family, and an idiomatic way to deal with both problems is while (scanf(...) == 5) { ... }
– Weather Vane
Nov 19 at 23:11
Your scanf statement should look like
scanf("%30s%30s%u%d%10s", ...)
.– eapetcho
Nov 20 at 4:13
Your scanf statement should look like
scanf("%30s%30s%u%d%10s", ...)
.– eapetcho
Nov 20 at 4:13
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
the following proposed code:
- cleanly compiles
- performs the desired functionality
- properly checks for errors
- eliminates the 'magic' numbers by using
#define
statements to give those magic numbers meaningful names - The call
scanf()
could be modified to incorporate the MAX CHARACTERS modifier(s) to be part of the parameter list, rather than hardcoded into the format string
And now, the proposed code:
#include <stdio.h>
#include <stdlib.h> // exit(), EXIT_FAILURE
#define MAX_FIRSTNAME_LEN 30
#define MAX_LASTNAME_LEN 30
#define MAX_ASSIGNMENT_LEN 10
int main( void )
{
FILE *cfPtr; // cfPtr = clients.txt file pointer
// fopen opens file. Exit program if unable to create file
if ((cfPtr = fopen("grades.txt", "w")) == NULL)
{
perror( "fopen to read grades.txt failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
puts("Enter the students first & last name, ID, grade, and assignment.");
puts("Enter EOF to end input.");
printf( "%s", "? " );
char firstName[ MAX_FIRSTNAME_LEN ]; // student first name
char lastName[ MAX_LASTNAME_LEN ]; // student last name
unsigned int stuID; // student ID
double grade; // student grade
char assignment[ MAX_ASSIGNMENT_LEN ]; // student assignment name
while( scanf( "%29s %29s %u %lf %9s", firstName, lastName, &stuID, &grade, assignment) == 5 )
{
fprintf( cfPtr,
"%st%st%dt%.2ft%stn",
firstName,
lastName,
stuID,
grade,
assignment);
printf( "%s", "? " );
} // end while
fclose( cfPtr ); // fclose closes file
} // end main
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
the following proposed code:
- cleanly compiles
- performs the desired functionality
- properly checks for errors
- eliminates the 'magic' numbers by using
#define
statements to give those magic numbers meaningful names - The call
scanf()
could be modified to incorporate the MAX CHARACTERS modifier(s) to be part of the parameter list, rather than hardcoded into the format string
And now, the proposed code:
#include <stdio.h>
#include <stdlib.h> // exit(), EXIT_FAILURE
#define MAX_FIRSTNAME_LEN 30
#define MAX_LASTNAME_LEN 30
#define MAX_ASSIGNMENT_LEN 10
int main( void )
{
FILE *cfPtr; // cfPtr = clients.txt file pointer
// fopen opens file. Exit program if unable to create file
if ((cfPtr = fopen("grades.txt", "w")) == NULL)
{
perror( "fopen to read grades.txt failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
puts("Enter the students first & last name, ID, grade, and assignment.");
puts("Enter EOF to end input.");
printf( "%s", "? " );
char firstName[ MAX_FIRSTNAME_LEN ]; // student first name
char lastName[ MAX_LASTNAME_LEN ]; // student last name
unsigned int stuID; // student ID
double grade; // student grade
char assignment[ MAX_ASSIGNMENT_LEN ]; // student assignment name
while( scanf( "%29s %29s %u %lf %9s", firstName, lastName, &stuID, &grade, assignment) == 5 )
{
fprintf( cfPtr,
"%st%st%dt%.2ft%stn",
firstName,
lastName,
stuID,
grade,
assignment);
printf( "%s", "? " );
} // end while
fclose( cfPtr ); // fclose closes file
} // end main
add a comment |
up vote
0
down vote
the following proposed code:
- cleanly compiles
- performs the desired functionality
- properly checks for errors
- eliminates the 'magic' numbers by using
#define
statements to give those magic numbers meaningful names - The call
scanf()
could be modified to incorporate the MAX CHARACTERS modifier(s) to be part of the parameter list, rather than hardcoded into the format string
And now, the proposed code:
#include <stdio.h>
#include <stdlib.h> // exit(), EXIT_FAILURE
#define MAX_FIRSTNAME_LEN 30
#define MAX_LASTNAME_LEN 30
#define MAX_ASSIGNMENT_LEN 10
int main( void )
{
FILE *cfPtr; // cfPtr = clients.txt file pointer
// fopen opens file. Exit program if unable to create file
if ((cfPtr = fopen("grades.txt", "w")) == NULL)
{
perror( "fopen to read grades.txt failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
puts("Enter the students first & last name, ID, grade, and assignment.");
puts("Enter EOF to end input.");
printf( "%s", "? " );
char firstName[ MAX_FIRSTNAME_LEN ]; // student first name
char lastName[ MAX_LASTNAME_LEN ]; // student last name
unsigned int stuID; // student ID
double grade; // student grade
char assignment[ MAX_ASSIGNMENT_LEN ]; // student assignment name
while( scanf( "%29s %29s %u %lf %9s", firstName, lastName, &stuID, &grade, assignment) == 5 )
{
fprintf( cfPtr,
"%st%st%dt%.2ft%stn",
firstName,
lastName,
stuID,
grade,
assignment);
printf( "%s", "? " );
} // end while
fclose( cfPtr ); // fclose closes file
} // end main
add a comment |
up vote
0
down vote
up vote
0
down vote
the following proposed code:
- cleanly compiles
- performs the desired functionality
- properly checks for errors
- eliminates the 'magic' numbers by using
#define
statements to give those magic numbers meaningful names - The call
scanf()
could be modified to incorporate the MAX CHARACTERS modifier(s) to be part of the parameter list, rather than hardcoded into the format string
And now, the proposed code:
#include <stdio.h>
#include <stdlib.h> // exit(), EXIT_FAILURE
#define MAX_FIRSTNAME_LEN 30
#define MAX_LASTNAME_LEN 30
#define MAX_ASSIGNMENT_LEN 10
int main( void )
{
FILE *cfPtr; // cfPtr = clients.txt file pointer
// fopen opens file. Exit program if unable to create file
if ((cfPtr = fopen("grades.txt", "w")) == NULL)
{
perror( "fopen to read grades.txt failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
puts("Enter the students first & last name, ID, grade, and assignment.");
puts("Enter EOF to end input.");
printf( "%s", "? " );
char firstName[ MAX_FIRSTNAME_LEN ]; // student first name
char lastName[ MAX_LASTNAME_LEN ]; // student last name
unsigned int stuID; // student ID
double grade; // student grade
char assignment[ MAX_ASSIGNMENT_LEN ]; // student assignment name
while( scanf( "%29s %29s %u %lf %9s", firstName, lastName, &stuID, &grade, assignment) == 5 )
{
fprintf( cfPtr,
"%st%st%dt%.2ft%stn",
firstName,
lastName,
stuID,
grade,
assignment);
printf( "%s", "? " );
} // end while
fclose( cfPtr ); // fclose closes file
} // end main
the following proposed code:
- cleanly compiles
- performs the desired functionality
- properly checks for errors
- eliminates the 'magic' numbers by using
#define
statements to give those magic numbers meaningful names - The call
scanf()
could be modified to incorporate the MAX CHARACTERS modifier(s) to be part of the parameter list, rather than hardcoded into the format string
And now, the proposed code:
#include <stdio.h>
#include <stdlib.h> // exit(), EXIT_FAILURE
#define MAX_FIRSTNAME_LEN 30
#define MAX_LASTNAME_LEN 30
#define MAX_ASSIGNMENT_LEN 10
int main( void )
{
FILE *cfPtr; // cfPtr = clients.txt file pointer
// fopen opens file. Exit program if unable to create file
if ((cfPtr = fopen("grades.txt", "w")) == NULL)
{
perror( "fopen to read grades.txt failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
puts("Enter the students first & last name, ID, grade, and assignment.");
puts("Enter EOF to end input.");
printf( "%s", "? " );
char firstName[ MAX_FIRSTNAME_LEN ]; // student first name
char lastName[ MAX_LASTNAME_LEN ]; // student last name
unsigned int stuID; // student ID
double grade; // student grade
char assignment[ MAX_ASSIGNMENT_LEN ]; // student assignment name
while( scanf( "%29s %29s %u %lf %9s", firstName, lastName, &stuID, &grade, assignment) == 5 )
{
fprintf( cfPtr,
"%st%st%dt%.2ft%stn",
firstName,
lastName,
stuID,
grade,
assignment);
printf( "%s", "? " );
} // end while
fclose( cfPtr ); // fclose closes file
} // end main
answered Nov 20 at 7:16
user3629249
10.8k1914
10.8k1914
add a comment |
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%2f53383840%2fwrite-to-a-file-where-the-output-is-tabbed-instead-of-spaced%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
I would start by checking what
scanf
is returning. If you didn't scan the right number of items you should stop there. You should also test your input for when yo stop, not after the fact with!feof
. stackoverflow.com/questions/5431941/…– Retired Ninja
Nov 19 at 23:01
3
Why are you restricting the string inputs to 5 characters? What do you think happens to the rest of the truncated strings?
– Weather Vane
Nov 19 at 23:06
1
You have 30-character variables, but only scan 5 characters
– stark
Nov 19 at 23:11
2
Please see Why is
while ( !feof (file) )
always wrong?. You should always check the return value fromscanf
function family, and an idiomatic way to deal with both problems iswhile (scanf(...) == 5) { ... }
– Weather Vane
Nov 19 at 23:11
Your scanf statement should look like
scanf("%30s%30s%u%d%10s", ...)
.– eapetcho
Nov 20 at 4:13