Something like this.Code:do: try to read some stuff if there is some stuff, then: split the string display the resulting strings while there is some stuff.
Something like this.Code:do: try to read some stuff if there is some stuff, then: split the string display the resulting strings while there is some stuff.
You have to stop RIGHT AWAY. Once you type "char *gpa=", it's too late -- that is not right away any more. You cannot try to process the data if it isn't there.
Guys I appreciate all the help. I have tried the if statement with !=NULL and ==NULL in it's current location and still getting segmentation fault. I am pulling my hair out with this one.
I really do not know what to do here. Again, guys I truely appreciate your time with me.Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 100 int main() { FILE *fP; char str[N], *c; /*char *gpa = strchr(str, ' ');*/ if ((fP = fopen("names.dat", "r")) == NULL) { printf("%s not opened", "names.dat"); exit(EXIT_FAILURE); } do { c = fgets(str, N ,fP); if (c != NULL); char *gpa = strchr(str, ' '); *gpa++ = '\0'; printf("%s\t%s\n", str, gpa); } while (c != NULL); fclose(fP); return 0; }
Or more simply:Code:do { c = fgets(str, N ,fP); if (c != NULL) { char *gpa = strchr(str, ' '); *gpa++ = '\0'; printf("%s\t%s\n", str, gpa); } }while (c != NULL);
Code:while(fgets(str, N, fP)) { char *gpa = strchr(str, ' '); *gpa++ = '\0'; printf("%s\t%s\n", str, gpa); }
If you understand what you're doing, you're not learning anything.