B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Mostly Asked C Programming Questions – Files and Advanced Topics | LiveCodeProgramming

Mostly Asked C Programming Questions – Files and Advanced Topics

Q1. File open, read, write, closeफाइल खोलना, पढ़ना, लिखना, बंद करना

Files in C are handled using file pointers and functions like fopen(), fread(), fwrite(), fclose().
Example to open and close a file:

FILE *fp = fopen("file.txt", "r");
if (fp == NULL) {
  perror("Error opening file");
  return 1;
}
fclose(fp);

C में फाइलों को फाइल पॉइंटर्स और fopen(), fread(), fwrite(), fclose() जैसे फंक्शंस के जरिए हैंडल किया जाता है।
फाइल खोलने और बंद करने का उदाहरण:

FILE *fp = fopen("file.txt", "r");
if (fp == NULL) {
  perror("फाइल खोलने में त्रुटि");
  return 1;
}
fclose(fp);

Q2. fscanf() vs scanf()fscanf() बनाम scanf()

scanf() reads input from standard input (keyboard), whereas fscanf() reads formatted input from a file stream.
Example:

fscanf(fp, "%d", &num); // reads integer from file
scanf("%d", &num);      // reads integer from keyboard

scanf() स्टैण्डर्ड इनपुट (कीबोर्ड) से डेटा पढ़ता है, जबकि fscanf() फाइल स्ट्रीम से फॉर्मेटेड इनपुट पढ़ता है।
उदाहरण:

fscanf(fp, "%d", &num); // फाइल से इंटीजर पढ़ना
scanf("%d", &num);      // कीबोर्ड से इंटीजर पढ़ना

Q3. fseek() and ftell()fseek() और ftell()

fseek() sets the file position indicator to a specific location, while ftell() returns the current file position.
Example:

fseek(fp, 0, SEEK_END); // move to end of file
long size = ftell(fp);   // get current position (file size)

fseek() फाइल पोजीशन इंडिकेटर को किसी विशिष्ट स्थान पर सेट करता है, जबकि ftell() वर्तमान फाइल पोजीशन लौटाता है।
उदाहरण:

fseek(fp, 0, SEEK_END); // फाइल के अंत में जाना
long size = ftell(fp);   // वर्तमान पोजीशन प्राप्त करना (फाइल का आकार)

Q4. Text vs binary filesटेक्स्ट बनाम बाइनरी फाइलें

Text files store data as readable characters, whereas binary files store data in raw binary form, which is efficient but not human-readable.
Text mode uses formats like \n for new lines; binary mode reads/writes bytes as-is.

टेक्स्ट फाइलें डेटा को पढ़ने योग्य अक्षरों के रूप में स्टोर करती हैं, जबकि बाइनरी फाइलें डेटा को कच्चे बाइनरी रूप में स्टोर करती हैं, जो प्रभावी लेकिन मानव-पठनीय नहीं होता।
टेक्स्ट मोड में नई लाइन के लिए \n जैसे फॉर्मेट होते हैं; बाइनरी मोड में बाइट्स वैसे ही पढ़े/लिखे जाते हैं।

Q5. File modes in fopen()fopen() में फाइल मोड

Common fopen() modes:
- "r": read
- "w": write (truncate file)
- "a": append
- "rb", "wb", "ab": binary modes for read, write, append respectively

fopen() के सामान्य मोड:
- "r": पढ़ना
- "w": लिखना (फाइल को साफ़ करता है)
- "a": जोड़ना
- "rb", "wb", "ab": बाइनरी मोड पढ़ने, लिखने, जोड़ने के लिए

Q6. Dynamic memory allocationडायनामिक मेमोरी आवंटन

C supports dynamic memory using malloc(), calloc(), realloc(), and free() functions to allocate and manage memory during runtime.

Example:

int *ptr = (int*)malloc(5 * sizeof(int));
if (ptr == NULL) { perror("Memory allocation failed"); exit(1); }

C में डायनामिक मेमोरी के लिए malloc(), calloc(), realloc(), और free() फंक्शंस का उपयोग होता है जो रनटाइम में मेमोरी आवंटित और प्रबंधित करते हैं।

उदाहरण:

int *ptr = (int*)malloc(5 * sizeof(int));
if (ptr == NULL) { perror("मेमोरी आवंटन विफल"); exit(1); }

Q7. malloc(), calloc(), realloc(), free()malloc(), calloc(), realloc(), free()

  • malloc(size): allocates uninitialized memory
  • calloc(n, size): allocates zero-initialized memory for n elements
  • realloc(ptr, new_size): changes size of previously allocated memory
  • free(ptr): deallocates memory

  • malloc(size): बिना इनिशियलाइज़ किए मेमोरी आवंटित करता है
  • calloc(n, size): n एलिमेंट्स के लिए ज़ीरो इनिशियलाइज़्ड मेमोरी आवंटित करता है
  • realloc(ptr, new_size): पहले से आवंटित मेमोरी का आकार बदलता है
  • free(ptr): मेमोरी मुक्त करता है

Q8. Dangling pointerडैंगलिंग पॉइंटर

A dangling pointer points to memory that has been freed or deallocated. Using dangling pointers can cause undefined behavior and program crashes.
Always set pointers to NULL after freeing.

Dangling pointer ऐसी पॉइंटर होती है जो मेमोरी की ओर इशारा करती है जो पहले ही फ्री या डीलोकेट हो चुकी है। इसका उपयोग अनिर्धारित व्यवहार और क्रैश का कारण बन सकता है।
मेमोरी फ्री करने के बाद पॉइंटर को NULL सेट करें।

Q9. Command line argumentsकमांड लाइन आर्गुमेंट्स

main() can accept command line arguments using parameters: int main(int argc, char *argv[]).

argc: number of arguments.
argv: array of argument strings.

Example:

int main(int argc, char *argv[]) {
  printf("Program name: %s\n", argv[0]);
  if (argc > 1) {
    printf("First argument: %s\n", argv[1]);
  }
}

main() कमांड लाइन आर्गुमेंट्स को पैरामीटर के रूप में ले सकता है: int main(int argc, char *argv[])

argc: आर्गुमेंट्स की संख्या।
argv: आर्गुमेंट स्ट्रिंग्स की एरे।

उदाहरण:

int main(int argc, char *argv[]) {
  printf("प्रोग्राम नाम: %s\n", argv[0]);
  if (argc > 1) {
    printf("पहला आर्गुमेंट: %s\n", argv[1]);
  }
}

Q10. Header files and their useहेडर फाइलें और उनका उपयोग

Header files (.h) contain function declarations, macros, and definitions. They are included using #include directive to reuse code and interface with libraries.
Common headers: stdio.h, stdlib.h, string.h.

हेडर फाइलें (.h) फंक्शन डिक्लेरेशन, मैक्रोज़ और परिभाषाएँ होती हैं। इन्हें #include डायरेक्टिव के साथ शामिल किया जाता है ताकि कोड पुनः उपयोग किया जा सके और लाइब्रेरी से इंटरफेस किया जा सके।
सामान्य हेडर्स: stdio.h, stdlib.h, string.h