Bird
Raised Fist0
Cprogramming~10 mins

File pointers in C - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to open a file named "data.txt" for reading.

C
#include <stdio.h>

int main() {
    FILE *fp;
    fp = fopen("data.txt", "[1]");
    if (fp == NULL) {
        printf("Failed to open file.\n");
        return 1;
    }
    fclose(fp);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aa
Bw
Cr
Drw
Attempts:
3 left
💡 Hint
Common Mistakes
Using "w" which overwrites the file instead of reading.
Using "rw" which is not a valid mode in C.
2fill in blank
medium

Complete the code to write the string "Hello" to the file using fprintf.

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("output.txt", "w");
    if (fp == NULL) {
        return 1;
    }
    fprintf(fp, "[1]\n");
    fclose(fp);
    return 0;
}
Drag options to blanks, or click blank then click option'
AWorld
BText
CFile
DHello
Attempts:
3 left
💡 Hint
Common Mistakes
Writing a different string than "Hello".
Forgetting to include the newline character if needed.
3fill in blank
hard

Fix the error in the code to correctly read a line from the file into buffer.

C
#include <stdio.h>

int main() {
    char buffer[100];
    FILE *fp = fopen("input.txt", "r");
    if (fp == NULL) {
        return 1;
    }
    fgets([1], 100, fp);
    printf("%s", buffer);
    fclose(fp);
    return 0;
}
Drag options to blanks, or click blank then click option'
Abuffer
B&buffer
Cfp
D*buffer
Attempts:
3 left
💡 Hint
Common Mistakes
Using &buffer which is a pointer to the array, not a char pointer.
Using *buffer which is the first character, not a pointer.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

C
#include <stdio.h>
#include <string.h>

int main() {
    char *words[] = {"cat", "elephant", "dog", "giraffe"};
    int lengths[4];
    int count = 0;
    for (int i = 0; i < 4; i++) {
        if (strlen(words[i]) [1] 3) {
            lengths[count] = strlen(words[i]);
            count[2];
        }
    }
    for (int j = 0; j < count; j++) {
        printf("%d\n", lengths[j]);
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A>
B++
C<
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than operator which selects wrong words.
Decrementing count instead of incrementing.
5fill in blank
hard

Fill all three blanks to create a file pointer, open a file for appending, and write a line.

C
#include <stdio.h>

int main() {
    FILE *[1];
    [1] = fopen("log.txt", "[2]");
    if ([1] == NULL) {
        return 1;
    }
    fprintf([1], "Log entry\n");
    fclose([1]);
    return 0;
}
Drag options to blanks, or click blank then click option'
Afp
Ba
Dr
Attempts:
3 left
💡 Hint
Common Mistakes
Using "r" mode which opens file for reading only.
Using different variable names inconsistently.

Practice

(1/5)
1. What does a file pointer in C represent?
easy
A. A variable that stores file contents
B. A reference to a file opened in the program
C. A function to read files
D. A type of file format

Solution

  1. Step 1: Understand what a file pointer is

    A file pointer is a special pointer that points to a file stream opened by the program.
  2. Step 2: Differentiate from other options

    It is not the file contents or a function, but a reference to the file for reading or writing.
  3. Final Answer:

    A reference to a file opened in the program -> Option B
  4. Quick Check:

    File pointer = Reference to opened file [OK]
Hint: File pointer points to file stream, not contents [OK]
Common Mistakes:
  • Thinking file pointer stores file data
  • Confusing file pointer with file reading function
  • Assuming file pointer is a file format
2. Which of the following is the correct way to open a file for reading in C?
easy
A. FILE *fp = fopen("file.txt", "r");
B. FILE fp = fopen("file.txt", "r");
C. fopen FILE *fp = "file.txt", "r";
D. FILE *fp = open("file.txt", "r");

Solution

  1. Step 1: Recall fopen syntax

    The fopen function returns a pointer to FILE, so the variable must be FILE *fp.
  2. Step 2: Check each option

    FILE *fp = fopen("file.txt", "r"); correctly declares FILE *fp and calls fopen with filename and mode "r".
  3. Final Answer:

    FILE *fp = fopen("file.txt", "r"); -> Option A
  4. Quick Check:

    Correct fopen syntax = FILE *fp = fopen("file.txt", "r"); [OK]
Hint: fopen returns FILE pointer, so use FILE * variable [OK]
Common Mistakes:
  • Declaring FILE variable instead of pointer
  • Using open() instead of fopen()
  • Incorrect order of fopen parameters
3. What will be the output of this C code snippet?
FILE *fp = fopen("test.txt", "w");
fprintf(fp, "Hello\n");
fclose(fp);

fp = fopen("test.txt", "r");
char buffer[10];
fgets(buffer, 10, fp);
printf("%s", buffer);
fclose(fp);
medium
A. Hello\n
B. Compilation error
C. test.txt
D. Hello

Solution

  1. Step 1: Write and close file

    The code opens test.txt for writing, writes "Hello\n", then closes the file.
  2. Step 2: Read and print file content

    It reopens test.txt for reading, reads a line into buffer, then prints it. fgets reads until newline or buffer limit.
  3. Final Answer:

    Hello (printed as Hello with newline) -> Option D
  4. Quick Check:

    File write/read prints "Hello" [OK]
Hint: fgets reads line including newline, printf prints it [OK]
Common Mistakes:
  • Expecting literal \n printed
  • Confusing fprintf and printf output
  • Forgetting to close file before reopening
4. Identify the error in this code snippet:
FILE *fp;
fp = fopen("data.txt", "r");
char c = fgetc(fp);
while (c != EOF) {
    putchar(c);
    c = fgetc(fp);
}
fclose(fp);
medium
A. fgetc returns int, not char, causing wrong EOF check
B. fopen mode "r" is incorrect for reading
C. Missing fclose call
D. putchar cannot print characters

Solution

  1. Step 1: Check fgetc return type

    fgetc returns int to allow EOF (-1) detection, storing in char loses EOF info.
  2. Step 2: Understand EOF check failure

    Comparing char c to EOF fails because c cannot hold EOF value properly, causing infinite loop or wrong output.
  3. Final Answer:

    fgetc returns int, not char, causing wrong EOF check -> Option A
  4. Quick Check:

    Use int for fgetc result to detect EOF [OK]
Hint: Store fgetc result in int to detect EOF correctly [OK]
Common Mistakes:
  • Using char instead of int for fgetc result
  • Ignoring EOF check causing infinite loops
  • Assuming putchar can't print chars
5. You want to append text "World" to an existing file "hello.txt" without deleting its content. Which is the correct way to open the file?
hard
A. FILE *fp = fopen("hello.txt", "r");
B. FILE *fp = fopen("hello.txt", "w");
C. FILE *fp = fopen("hello.txt", "a");
D. FILE *fp = fopen("hello.txt", "r+");

Solution

  1. Step 1: Understand file modes

    "a" mode opens file for appending, preserving existing content and writing at the end.
  2. Step 2: Compare other modes

    "w" overwrites file, "r" is read-only, "r+" allows read/write but does not append automatically.
  3. Final Answer:

    FILE *fp = fopen("hello.txt", "a"); -> Option C
  4. Quick Check:

    Append mode = "a" [OK]
Hint: Use "a" mode to add without erasing file [OK]
Common Mistakes:
  • Using "w" which erases file content
  • Using "r" which is read-only
  • Confusing "r+" with append mode
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard