confused by C syntax -
i'm new @ c.. , still having trouble @ syntax, hope can me... cause i'm stuck @ code
#include<stdio.h> #include<stdlib.h> #include<string.h> int main(void){ file *stream = null; stream = fopen("studentinfo.txt", "a+"); /*some of initialization used testing purposes only*/ char arr[5]; char arr2[5]; int i; char name[3]; char course[5]; printf("enter details: "); scanf("%s", arr2); while(!feof(stream)){ fgets(arr, 100, stream);//i confused if line capture stored @ arr[0] if(strcmp(arr, arr2)==0){//i want compare printf("success");//testing } printf("%s", arr);//i wonder fgets overwrites new line arr[0] } fclose(stream); getch(); } thanks guys...
if have existing file... , file has data on it. check if data typed existing on file or not. i'm not sure if want.
example if typed... love , file contains exact word... love (on 1 line) print "success".
if data typed not existing on file, appended on file (on next line).
int main(void){ char arr[5]; char arr2[5]; int i; int n=0; file *stream = null; file *append = null; stream = fopen("studentinfo.txt", "rt"); append = fopen("studentinfo.txt", "a+"); printf("enter details: "); scanf("%s", arr2); while(!feof(stream)){ fgets(arr, 6, stream); if(strcmp(arr, arr2)==0){ printf("success"); } else n=-1; } if (n==-1){ fprintf(append, "%s\n", arr2); } fclose(stream); fclose(append); system("pause"); }
Comments
Post a Comment