#include #include #include struct person { char name[10]; int age; float weight; }; int main() { FILE *fd; // Open the file for writing only fd = fopen("sampleFile.txt", "w"); if (!fd) { printf("ERROR: Could not open file\n"); exit(1); } // Write out a float float data = 1078.24; fprintf(fd, "%f\n", data); // Write out a string of 30 chars char sentence[30] = "This is a sentence."; fprintf(fd, "%-30s\n", sentence); // Write out a whole array of ints int intArray[5] = {12, 34, 56, 78, 90}; for (int i=0; i<5; i++) fprintf(fd, "%d ", intArray[i]); fprintf(fd, "\n"); // Write out first three elements of the array for (int i=0; i<3; i++) fprintf(fd, "%d ", intArray[i]); fprintf(fd, "\n"); // Write out an array of structs struct person friends[4] = {{"Bobby", 19, 143.57}, {"Jenny", 20, 110.32}, {"Fredy", 82, 178.29}, {"Marie", 67, 121.32}}; for (int i=0; i<4; i++) fprintf(fd, "%s %d %f\n", friends[i].name,friends[i].age,friends[i].weight); // Write out a single friend fprintf(fd, "%s %d %f\n", friends[0].name,friends[0].age,friends[0].weight); // All done ... close the file fclose(fd); }