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 |
What is break statement?
what are non standard function in c
What is restrict keyword in c?
Why c is called procedure oriented language?
If errno contains a nonzero number, is there an error?
What is #include conio h?
Table of Sudoku n*n
In C language, a variable name cannot contain?
How does normalization of huge pointer works?
How can you determine the size of an allocated portion of memory?
who is first prime minister in india??
Is it possible to execute code even after the program exits the main() function?