Code:#include <stdio.h> #include <stdlib.h> #include <string.h> struct str_in{ char str[100]; int num; }; int main() { struct str_in str_in_ar[3]; int i; char ap_str[200]; FILE *fp; for(i=0;i<3;i++) { printf("Enter a string(100 character limit): "); fflush(stdin); gets(str_in_ar[i].str); printf("Enter a number: "); fflush(stdin); scanf("%d",&str_in_ar[i].num); } printf("Writing it to file....."); fp = fopen("str_1.txt","a"); if(fp) { for(i=0;i<3;i++) strcpy(ap_str,str_in_ar[i].str); strcat(ap_str, " "); strcat(ap_str, (char)str_in_ar[i].num); //strcat(ap_str, "\n"); fputs(ap_str,fp); memset(ap_str,0,200); fclose(fp); } return 0; }
With this code what i'm trying to do is to write a line with a string followed by integer and then similarly to the few lines into a txt file.
i hope this ->( .Screenshot by Lightshot ) makes it clear.