Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Homework 13
- Only submit the .c file.
- Write a C program that opens the employees.txt file for reading.
- This file must be added to your project.
- Write a loop that reads all employees from the file and prints the data to the console.
- Close the file.
- HINT: If you are having issues retrieving the name after the first time in the loop,
- try entering a space after the format specifier in the last call to fscanf within the loop.
- i.e. It should be something like this fscanf(filePtr, "%f ", &someFloat); or use fgetc()
- */
- #pragma warning(disable : 4996)
- #include <stdio.h>
- #define FIELD_SIZE 30
- #define NAME_SIZE 30
- int main()
- {
- FILE *fp; //Declare a FILE pointer variale
- fp = fopen("employees.txt", "r"); //fopen opens the txt file named in quotes, and "r" tells it to red the file as data type
- char c; //declare a char var named c
- while ((c = getc(fp)) != EOF) //This while loop takes the next char, and then cycles until the built in EOF end of file function. If yes, the code does not enter the loop. If no, the first line places the char onto the stream.
- {
- ungetc(c, fp);
- char name[NAME_SIZE];
- char field[FIELD_SIZE];
- float decimal;
- fgets(name, NAME_SIZE, fp);
- printf("%s", name);
- fgets(field, FIELD_SIZE, fp);
- printf("%s", field);
- fscanf(fp, "%f ", &decimal);
- printf("%.2f\n", decimal);
- }
- fclose(fp); //This closes the file when done
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement