Explain how can I read and write comma-delimited text?



Explain how can I read and write comma-delimited text?..

Answer / Purshottam Singh

In C, you can use functions like `fopen`, `fread`, and `fwrite` from the `stdio.h` library to read and write comma-separated values (CSV) from a file. Here's an example of how to read data from a CSV file:

```c
#include <stdio.h>
#include <stdlib.h>

void read_csv(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error: Unable to open file.n");
return;
}

char line[1024];
while (fgets(line, sizeof(line), file)) {
// Process each line
}

fclose(file);
}
```

To write data to a CSV file, you can modify the example above and use `fprintf` instead of `fgets`. Remember to escape commas in your data using backslashes (e.g., `,`) if necessary.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

What is break statement?

1 Answers  


what are non standard function in c

1 Answers  


What is restrict keyword in c?

1 Answers  


Why c is called procedure oriented language?

1 Answers  


If errno contains a nonzero number, is there an error?

1 Answers  


What is #include conio h?

1 Answers  


Table of Sudoku n*n

0 Answers  


In C language, a variable name cannot contain?

1 Answers  


How does normalization of huge pointer works?

1 Answers  


How can you determine the size of an allocated portion of memory?

1 Answers   Aspire, Infogain,


who is first prime minister in india??

8 Answers   Wipro,


Is it possible to execute code even after the program exits the main() function?

1 Answers  


Categories