-
Data missing in variable
I'm willing to bet everyone is sick of me :D . Data is missing or being passed over for the variable s->curr. I'm sending all the data to an output file and don't see s->curr being printed. I'm reading an input file where the data is seperated by colons. Heres my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
struct stuinfo {
char name[30];
int ssn;
char dob[11];
char curr[4];
int gradyr;
};
int y;
char buf[80], *p, *x; /* file buffer area */
FILE *in; /* input file stream pointer */
FILE *out; /* output file stream pointer */
struct stuinfo *s;
s = malloc(sizeof(struct stuinfo));
in = fopen("d6.dat", "r");
if(in == NULL) {
perror("fopen: d6.dat");
exit(1);
}
out = fopen("d6.out", "w");
if(out == NULL) {
perror("fopen: d6.out");
exit(1);
}
fgets(buf, 80, in);
fprintf(out, "Student Name SSN DOB Curr Grad Yr\n");
fprintf(out, "------------ --- --- ---- -------\n");
while(! feof(in)) {
p = buf;
x = strchr(p,':');
*x = '\0';
strcpy(s->name, p);
p = x + 1;
x = strchr(p,':');
strcpy(s->curr, p);
p = x + 1;
x = strchr(p,':');
s->gradyr = atoi(p);
p = x + 1;
x = strchr(p,':');
s->ssn = atoi(p);
p = x + 1;
x = strchr(p,':');
strcpy(s->dob, p);
p = x + 1;
x = strchr(p,':');
fprintf(out, "%-25s", s->name);
fprintf(out, "%-3d-%2d-%4d", s->ssn / 1000000, (s->ssn / 10000) % 100, s->ssn % 10000);
for(y = 0;y < 3;y++)
fputc(' ', out);
fputs(s->dob, out);
fprintf(out, "%s", s->curr);
fprintf(out, "%d\n", s->gradyr);
fgets(buf, 80, in);
}
fclose(out);
fclose(in);
free(s);
printf ("Press ENTER to continue.\n");
getchar ();
return 0;
}
-
> x = strchr(p,':');
> strcpy(s->curr, p);
> x = strchr(p,':');
> strcpy(s->dob, p);
You're not terminating your strings before you use strcpy().
Code:
x = strchr(p,':');
*x = '\0';
strcpy(s->curr, p);
x = strchr(p,':');
*x = '\0';
strcpy(s->dob, p);
Also s->curr only has room for 3 characters, plus the string terminator, so make sure it's only three characters long, or declare it bigger.
-
I add the *x = '\0'; before the strcpy and I get a coredump.
-
Paste
- your latest code
- some example data in your file.
-
My latest code, which I don't think has changed from the first post:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
struct stuinfo {
char name[30];
int ssn;
char dob[11];
char curr[4];
int gradyr;
};
int y;
char buf[80], *p, *x; /* file buffer area */
FILE *in; /* input file stream pointer */
FILE *out; /* output file stream pointer */
struct stuinfo *s;
s = malloc(sizeof(struct stuinfo));
in = fopen("d6.dat", "r");
if(in == NULL) {
perror("fopen: d6.dat");
exit(1);
}
out = fopen("d6.out", "w");
if(out == NULL) {
perror("fopen: d6.out");
exit(1);
}
fgets(buf, 80, in);
fprintf(out, "Student Name SSN DOB Curr Grad Yr\n");
fprintf(out, "------------ --- --- ---- -------\n");
while(! feof(in)) {
p = buf;
x = strchr(p,':');
*x = '\0';
strcpy(s->name, p);
p = x + 1;
x = strchr(p,':');
strcpy(s->curr, p);
p = x + 1;
x = strchr(p,':');
s->gradyr = atoi(p);
p = x + 1;
x = strchr(p,':');
s->ssn = atoi(p);
p = x + 1;
x = strchr(p,':');
strcpy(s->dob, p);
p = x + 1;
x = strchr(p,':');
fprintf(out, "%-25s", s->name);
fprintf(out, "%-3d-%2d-%4d", s->ssn / 1000000, (s->ssn / 10000) % 100, s->ssn % 10000);
for(y = 0;y < 3;y++)
fputc(' ', out);
fputs(s->dob, out);
fprintf(out, "%s", s->curr);
fprintf(out, "%d\n", s->gradyr);
fgets(buf, 80, in);
}
fclose(out);
fclose(in);
free(s);
printf ("Press ENTER to continue.\n");
getchar ();
return 0;
}
Some example data:
Code:
Tim Smith:ACT:2008:451237458:04-15-1981
Jen Hoover:NUR:2007:895621478:11-06-1979
Tim Schneebly:MUS:2006:123054697:12-25-1974
Tina Rimmer:SCI:2010:756213025:03-14-1983
Fred Rogers:REL:2011:456158743:05-13-1985
-
> *x = '\0';
You're missing all of these after the first field.
> x = strchr(p,':');
> strcpy(s->dob, p);
http://cboard.cprogramming.com/showthread.php?t=62654
Note that your last field ends with '\n', not ':'
-
Adding *x = '\0'; coredumps. The last field ends with '\n' doesn't explain why s->curr is not getting its data.
-
If strchr doesn't find what it's looking for, it returns NULL, and doing *x = '\0'; on such a result is bad news.
Are you sure all your input data is valid?
Well looking at your code, you have one too many strchr calls.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stuinfo {
char name[30];
int ssn;
char dob[11];
char curr[4];
int gradyr;
};
void decode(char *buf)
{
FILE *out = stdout;
char *p, *x;
struct stuinfo stu;
struct stuinfo *s = &stu;
p = buf;
x = strchr(p, ':');
*x = '\0';
strcpy(s->name, p);
p = x + 1;
x = strchr(p, ':');
*x = '\0';
strcpy(s->curr, p);
p = x + 1;
x = strchr(p, ':');
*x = '\0';
s->gradyr = atoi(p);
p = x + 1;
x = strchr(p, ':');
*x = '\0';
s->ssn = atoi(p);
p = x + 1;
x = strchr(p, '\n');
*x = '\0';
strcpy(s->dob, p);
fprintf(out, "%-25s", s->name);
fprintf(out, "%-3d-%2d-%4d", s->ssn / 1000000, (s->ssn / 10000) % 100,
s->ssn % 10000);
fprintf(out, " %s", s->dob);
fprintf(out, " %s", s->curr);
fprintf(out, " %d\n", s->gradyr);
}
int main()
{
char *tests[] = {
"Tim Smith:ACT:2008:451237458:04-15-1981\n",
"Jen Hoover:NUR:2007:895621478:11-06-1979\n",
"Tim Schneebly:MUS:2006:123054697:12-25-1974\n",
"Tina Rimmer:SCI:2010:756213025:03-14-1983\n",
"Fred Rogers:REL:2011:456158743:05-13-1985\n",
};
size_t i;
for ( i = 0 ; i < sizeof(tests)/sizeof(*tests); i++ ) {
char temp[100];
strcpy( temp, tests[i] );
decode( temp );
}
return 0;
}
My results
Code:
Tim Smith 451-23-7458 04-15-1981 ACT 2008
Jen Hoover 895-62-1478 11-06-1979 NUR 2007
Tim Schneebly 123- 5-4697 12-25-1974 MUS 2006
Tina Rimmer 756-21-3025 03-14-1983 SCI 2010
Fred Rogers 456-15-8743 05-13-1985 REL 2011
-
I stepped a way for while and then it hit me like a ton of brick as what you were saying about the data ending with a '\n'. I thank you for your patience and help.