Files Flashcards

(21 cards)

1
Q

Why do we use files in C programming?

A

For permanent storage of data, to read and process large amounts of data, and to save program output permanently.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a FILE pointer and how is it declared?

A

A special pointer that keeps track of the current position within a file. Declared as FILE *fp;.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List three file opening modes and their purpose.

A

“r”: Read mode (file must exist).
“w”: Write mode (creates new file or overwrites existing).
“a”: Append mode (creates new file or adds to end of existing).
“r+”: Read and write (file must exist).
“w+”: Read and write (creates/overwrites).
“a+”: Read and append.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between fprintf() and fputs()?

A

fprintf(): Writes formatted output to a file (like printf).

fputs(): Writes a string to a file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you read a file character by character until the end of the file?

A

Using a while loop with fgetc() and checking for EOF.
e.g., while ((ch = fgetc(fp)) != EOF) { … }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of fseek() and ftell()?

A

fseek(): Repositions the file pointer to a specific location within the file.
ftell(): Returns the current position of the file pointer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you close the file associated with the file pointer fp?

A

fclose(fp);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you write the string “Hello File!” to a file using fprintf() and fputs() with a file pointer fp?

A

fprintf(fp, “Hello File!\n”);

fputs(“Hello File!\n”, fp);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you read a single character from a file pointed to by fp into ch, and what value indicates the end of the file?

A

char ch = fgetc(fp); The end of the file is indicated by EOF.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you read an entire line from a file pointed to by fp into a character array line (max 100 characters)?

A

fgets(line, 100, fp);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you move the file pointer fp to the beginning of the file using fseek()?

A

fseek(fp, 0, SEEK_SET);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Format of fseek function

A

int fseek(file pointer, offset, reference point)
SEEK_SET - Beginning of file
SEEK_CUR - Current position of the file pointer
SEEK_END - End of file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Text file vs binary file

A

Text file can be opened in a simple editor such as notepad.

Binary file cannot be opened without a specific application software. For example EXE file, JPG, MP3, PDF etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Syntax of opening file

A

FILE *fp = fopen(“example.txt”, “w”);
if (fp == NULL) {
printf(“File cannot be opened.\n”);
} else {
printf(“File opened successfully.\n”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

close file syntax

A

fclose(fp);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

printing number to file syntax

A

fprintf(fp, “The number is %d\n”, num);

17
Q

write a single character

A

fputc(‘A’, fp);
fputc(‘\n’, fp);

18
Q

write a string

A

fputs(“Hello, World!”, fp);

19
Q

Using fscanf() – formatted input (like scanf)

A

fscanf(fp, “The number is %d”, &x);

20
Q

Using fgetc() – read one character at a time

A

FILE *fp = fopen(“char.txt”, “r”);
char ch = fgetc(fp);
printf(“Character: %c\n”, ch);
fclose(fp);

21
Q

Using fgets() – read string (line by line)

A

FILE *fp = fopen(“string.txt”, “r”);
char line[100];
fgets(line, sizeof(line), fp);
printf(“Line: %s\n”, line);
fclose(fp);