i try hard but i couldn`t fix the problem?!
This is the source code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char *sn;
char *cn;
struct node* next;
};
typedef struct node N;
typedef N* NP;
int main()
{
char s[100];
char *r=NULL;
char d[1];
d[0]=':';
int i,x,y,j;
j=0;
NP root;
NP t;
int c=0;
FILE *fp;
x=0;
y=0;
//File Checking !
fp = fopen("c:\\textfiles/test.txt","r");
if(fp == NULL)
{
printf("file couldn`t be opened!\n");
exit(0);
}
fgets(s,100,fp);
j=0;
d[0] = ':';
for ( i = 0; i < 100; i++ )
{
if ( s[i] == '\n' )
{
s[i] = '\0';
break;
}
}
r = strtok(s,d);
r = strtok(NULL,d);
d[0] = ',';
if(s[9] == '1')
{
while(j<3)
{
if(j==0)
{
r = strtok(r,d);
}
t = (N*) malloc(sizeof(N));
if(j==0)
root=t;
t->sn = "s1";
t->cn = r;
t->next =0;
r = strtok(NULL,d);
++j;
if(j<3)
t=t->next;
}
}
t->next = 0;
while(c<3)
{
printf("%s,",root->cn);
root = root->next;
}
printf("\n")
fclose(fp);
return 0;
}
and this is my input file :
Code:
student s1 takes exams:csc212,csc281,csc361
after compiling the program it will output
csc212, [the program crash]
my compiler is : Microsoft Visual C++