save file error!
-
hi ,I'm a freshman ,and newer to study c !this is my program! #include <stdio.h> #include <stdlib.h> #include <string.h> #define STOP 0 #define TRUE 1 #define OK 1 #define FILE_NAME ("my.text") typedef struct List { long lNum; char cpsName[30]; long lCScore; long lMathScore; long atol(long i); struct List *next; }Stu; Stu stuList[3] = { {1, "asd", 32, 44, 0}, {2, "ffg", 66, 66, 0}, {3, "dgd", 55, 44, 0}, }; Stu *CreateList() { Stu *head = NULL; Stu *NewNode = NULL; int i = 0; for (i=0; i<3; i++) { NewNode = (Stu*)malloc(sizeof(Stu)); NewNode->lNum = stuList[i].atol(stuList[i].lNum); strncpy(NewNode->cpsName, stuList[i].cpsName, 30); NewNode->lCScore = stuList[i].atol(stuList[i].lCScore); NewNode->lMathScore = stuList[i].atol(stuList[i].lMathScore); NewNode->next = NULL; if (NULL == head) { head = NewNode; } else { head->next = NewNode; head = NewNode; } } return head; } int SaveToFiles(Stu *head) { Stu *p = head; FILE *fp; char cFileName[1024] = {0}; //strcpy(cFileName, FILE_NAME); if ((fp = fopen(FILE_NAME, "wb")) == NULL) { printf( "Open file error!\n" ); exit (1); } while (p != NULL) { if (fwrite(p, sizeof(Stu), 1, fp) == 1) { p = p->next; } else { printf("Write file error!\n"); exit(1); } } //fputs(p, cFileName); fclose(fp); printf( "Save file succeed!\n" ); return OK; } int OpenFiles() { char cFileName[256] = {0}; FILE *fp; if (0 == (fp = fopen(FILE_NAME, "r"))) { printf("Open file error!\n"); exit(0); } fgets(cFileName, 250, fp); printf("%s", cFileName); fclose(fp); return OK; } int main() { Stu *head = NULL; head = CreateList(); SaveToFiles(head); OpenFiles(); //Print(head); return 0; }
-
hi ,I'm a freshman ,and newer to study c !this is my program! #include <stdio.h> #include <stdlib.h> #include <string.h> #define STOP 0 #define TRUE 1 #define OK 1 #define FILE_NAME ("my.text") typedef struct List { long lNum; char cpsName[30]; long lCScore; long lMathScore; long atol(long i); struct List *next; }Stu; Stu stuList[3] = { {1, "asd", 32, 44, 0}, {2, "ffg", 66, 66, 0}, {3, "dgd", 55, 44, 0}, }; Stu *CreateList() { Stu *head = NULL; Stu *NewNode = NULL; int i = 0; for (i=0; i<3; i++) { NewNode = (Stu*)malloc(sizeof(Stu)); NewNode->lNum = stuList[i].atol(stuList[i].lNum); strncpy(NewNode->cpsName, stuList[i].cpsName, 30); NewNode->lCScore = stuList[i].atol(stuList[i].lCScore); NewNode->lMathScore = stuList[i].atol(stuList[i].lMathScore); NewNode->next = NULL; if (NULL == head) { head = NewNode; } else { head->next = NewNode; head = NewNode; } } return head; } int SaveToFiles(Stu *head) { Stu *p = head; FILE *fp; char cFileName[1024] = {0}; //strcpy(cFileName, FILE_NAME); if ((fp = fopen(FILE_NAME, "wb")) == NULL) { printf( "Open file error!\n" ); exit (1); } while (p != NULL) { if (fwrite(p, sizeof(Stu), 1, fp) == 1) { p = p->next; } else { printf("Write file error!\n"); exit(1); } } //fputs(p, cFileName); fclose(fp); printf( "Save file succeed!\n" ); return OK; } int OpenFiles() { char cFileName[256] = {0}; FILE *fp; if (0 == (fp = fopen(FILE_NAME, "r"))) { printf("Open file error!\n"); exit(0); } fgets(cFileName, 250, fp); printf("%s", cFileName); fclose(fp); return OK; } int main() { Stu *head = NULL; head = CreateList(); SaveToFiles(head); OpenFiles(); //Print(head); return 0; }
Please read the posting guidelines[^]. For instance, format your code properly using the code tag (otherwise, it is not readable). Furthermore, you didn't ask any question and you didn't provide any description of the problem you encounter.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
hi ,I'm a freshman ,and newer to study c !this is my program! #include <stdio.h> #include <stdlib.h> #include <string.h> #define STOP 0 #define TRUE 1 #define OK 1 #define FILE_NAME ("my.text") typedef struct List { long lNum; char cpsName[30]; long lCScore; long lMathScore; long atol(long i); struct List *next; }Stu; Stu stuList[3] = { {1, "asd", 32, 44, 0}, {2, "ffg", 66, 66, 0}, {3, "dgd", 55, 44, 0}, }; Stu *CreateList() { Stu *head = NULL; Stu *NewNode = NULL; int i = 0; for (i=0; i<3; i++) { NewNode = (Stu*)malloc(sizeof(Stu)); NewNode->lNum = stuList[i].atol(stuList[i].lNum); strncpy(NewNode->cpsName, stuList[i].cpsName, 30); NewNode->lCScore = stuList[i].atol(stuList[i].lCScore); NewNode->lMathScore = stuList[i].atol(stuList[i].lMathScore); NewNode->next = NULL; if (NULL == head) { head = NewNode; } else { head->next = NewNode; head = NewNode; } } return head; } int SaveToFiles(Stu *head) { Stu *p = head; FILE *fp; char cFileName[1024] = {0}; //strcpy(cFileName, FILE_NAME); if ((fp = fopen(FILE_NAME, "wb")) == NULL) { printf( "Open file error!\n" ); exit (1); } while (p != NULL) { if (fwrite(p, sizeof(Stu), 1, fp) == 1) { p = p->next; } else { printf("Write file error!\n"); exit(1); } } //fputs(p, cFileName); fclose(fp); printf( "Save file succeed!\n" ); return OK; } int OpenFiles() { char cFileName[256] = {0}; FILE *fp; if (0 == (fp = fopen(FILE_NAME, "r"))) { printf("Open file error!\n"); exit(0); } fgets(cFileName, 250, fp); printf("%s", cFileName); fclose(fp); return OK; } int main() { Stu *head = NULL; head = CreateList(); SaveToFiles(head); OpenFiles(); //Print(head); return 0; }