File I/O in C reading strings from a text file - c

I am having trouble reading all content of a single text file into a string. Whenever there is a line break it does not keep the previous strings.
For example if the file contained:
this is stack overflow
and it is cool
The only thing that the string would have after reading the file is " it is cool"
here is the code:
FILE *inputFilePtr;
inputFilePtr = fopen(inputFileName, "r");
char plainText[10000];
// if the file does not exist
if (inputFilePtr == NULL)
{
printf("Could not open file %s", inputFileName);
}
// read the text from the file into a string
while (fgets(plainText, "%s", inputFilePtr))
{
fscanf(inputFilePtr, "%s", plainText);
}
printf("%s\n", plainText);
fclose(inputFilePtr);
Any Help is greatly appreciated!

If you want to display all file contents try :
FILE *inputFilePtr;
inputFilePtr = fopen(inputFileName, "r");
char plainText[10000];
// if the file does not exist
if (inputFilePtr == NULL)
{
printf("Could not open file %s", inputFileName);
}
// read the text from the file into a string
while (!feof(inputFilePtr)) //while we are not at the end of the file
{
fgets(plainText, "%s", inputFilePtr);
printf("%s\n", plainText);
}
fclose(inputFilePtr);
Or if you want to display all file contents in one string use :
#include <string.h>
int main()
{
FILE *inputFilePtr;
inputFilePtr = fopen(inputFileName, "r");
char plainText[10000];
char buffer[10000];
// if the file does not exist
if (inputFilePtr == NULL)
{
printf("Could not open file %s", inputFileName);
}
// read the text from the file into a string
while (!feof(inputFilePtr))
{
fgets(buffer, "%s", inputFilePtr);
strcat(plainText,buffer);
}
printf("%s\n", plainText);
fclose(inputFilePtr);
}

Related

Running is fine but rename() does not work

Simple function code to delete a line from a text file by making a temporary text file that will store the new content once the line has been deleted and replacing the old Storage.txt file with the temporary file.
The delete() function works but my only problem seems to be the rename() function that seemingly won't do as intended.
THE CODE
void delete() {
struct task task;
FILE *fp;
char str[100];
char ch;
int delete_line = 0;
fp = fopen("Storage.txt", "r");
if (fp == NULL) {
printf("Error opening file");
fopen("Storage.txt", "w");
exit (1);
}
printf("\n\n\nAll Tasks\n");
printf("----------\n\n");
do {
ch = fgetc(fp);
printf("%c", ch);
} while (ch != EOF);
fclose(fp);
int line_no,ret;
char filename[] = "Storage.txt";
char newname[] = "temp.txt";
FILE *file, *temp;
file = fopen("Storage.txt", "r");
temp = fopen("temp.txt", "w");
printf("Select Line to delete: ");
scanf("d", &delete_line);
getchar();
temp = fopen("temp.txt", "w");
while (fgets(str, 99, fp) != NULL) {
line_no++;
if (line_no != delete_line) {
fputs(str, temp);
}
}
fclose(file);
fclose(temp);
remove(filename);
ret = rename(newname, filename);
if (ret == 0) {
printf("File renamed successfully");
} else {
printf("Error: unable to rename the file");
}
}
There are some problems in the code:
ch must be defined with type int to detect EOF reliably.
the do/while loop to read the file contents outputs the EOF indicator before testing it. You should use while ((ch = fgetc(fp)) != EOF) putchar(ch);
the identifier delete should be avoided to avoid confusing C++ programmers, use delete_line instead.
you should test for failure of fopen and remove and display the cause of the error.
if opening the file for reading fails, why do you create the file with fopen("Storage.txt", "w") ?
file temp.txt is open twice, which may prevent the rename operation on legacy systems.
line_no is not initialized. It should be initialized to 1 if lines are numbered starting at 1.
reading lines into an array is not reliable for this task as lines longer than 99 bytes will be counted more than once.
Here is a modified version:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void delete_line() {
const char *filename = "Storage.txt";
const char *tempname = "temp.txt";
int ch;
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "Cannot open file %s: %s\n", filename, strerror(errno));
exit(1);
}
printf("\n\n\nAll Tasks\n");
printf("----------\n\n");
while ((ch = getc(fp)) != EOF) {
putchar(ch);
}
fclose(fp);
int delete_line = 0;
printf("Select Line to delete: ");
if (scanf("d", &delete_line) != 1) {
fprintf(stderr, "invalid or missing input\n");
exit(1);
}
// read and discard the rest of the user input line
while ((ch = getchar()) != EOF && c != '\n')
continue;
FILE *file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "Cannot open file %s: %s\n", filename, strerror(errno));
exit(1);
}
FILE *temp = fopen(tempname, "w");
if (temp == NULL) {
fprintf(stderr, "Cannot open file %s: %s\n", tempname, strerror(errno));
fclose(file);
exit(1);
}
int line_no = 1;
while ((ch = getc(file)) != EOF) {
if (line_no != delete_line)
putc(ch, temp);
if (ch == '\n')
line_no++;
}
fclose(file);
fclose(temp);
if (remove(filename)) {
fprintf(stderr, "Cannot remove %s: %s\n", filename, strerror(errno));
} else {
if (rename(tempname, filename)) {
fprintf(stderr, "Cannot rename %s as %s: %s\n",
tempname, filename, strerror(errno));
}
}
}
Your code opens the "temp.txt" file twice:
temp = fopen("temp.txt", "w");
...
temp = fopen("temp.txt", "w");
And closes it once. That will leave one open file descriptor to the file, untill the program exits.
remove() uses unlink() for deleting files. The man page of unlink() says:
If the name was the last link to a file but any processes still have
the file open the file will remain in existence until the last file
descriptor referring to it is closed.
Ensure that all file descriptors are closed when not needed anymore.
The rename may fail, if file of oldpath or newpath is still open.
temp = fopen("temp.txt", "w"); Call it twice
The two main bugs here are:
1.
scanf("d", ...) instead of
scanf("%d", ...)
scanf() needs a format string to know how to parse the input, just like printf() (the f is for format) needs it to know how to construct the output; and their format string syntax is almost the same.
2.
Unintialized line_no, meaning that it's not guaranteed to start at 0/1, thus it might not ever be equal to delete_line, and will not delete the line.

C cannot write to file after reading

I have a function in the program that has to remove a given string from a file. To do this, rewrites the entire file into a temporary file and then overwrites the original file.
Saving a temporary file with the removed string works, but overwriting the original file does't work.
What's wrong here?
#define MAXCHAR 10000
void delPath(char stringToDelete[], char bashrcDir[]) {
FILE *bashrc = fopen(bashrcDir, "r+");
char str[MAXCHAR];
if (bashrc != NULL) {
FILE *tempfile = fopen("./tempFile.txt", "w+");
// Create tempFile and copy content without given string
while (fgets(str, MAXCHAR, bashrc) != NULL) {
if (!strstr(str, stringToDelete)) {
fprintf(tempfile, "%s", str);
}
}
// Read tempFile and overwrite original file - this doesn't work
while (fgets(str, MAXCHAR, tempfile) != NULL) {
fprintf(bashrc, "%s", str);
}
fclose(tempfile);
}
fclose(bashrc);
}
r+ allows you to read the file and overwrite it. I'm wrong?
Referring to #KamilCuk answer, here's the solution:
#define MAXCHAR 10000
void delPath(char stringToDelete[], char bashrcDir[]) {
FILE *bashrc = fopen(bashrcDir, "r");
char str[MAXCHAR];
if (bashrc != NULL) {
FILE *tempfile = fopen("./tempFile.txt", "w");
while (fgets(str, MAXCHAR, bashrc) != NULL) {
if (!strstr(str, stringToDelete)) {
fprintf(tempfile, "%s", str);
}
}
fclose(bashrc);
fclose(tempfile);
FILE *newTempfile = fopen("./tempFile.txt", "r");
FILE *newBashrc = fopen(bashrcDir, "w");
while (fgets(str, MAXCHAR, newTempfile) != NULL) {
fprintf(newBashrc, "%s", str);
}
fclose(newTempfile);
fclose(newBashrc);
remove("./tempFile.txt");
}
}
Thanks!

Why is my file pointer always NULL, even with text in it?

I am having trouble opening and parsing a file in C. As of now I am trying to open a file and print out the current line then the next until the program reaches the end of the file. However, the file pointer always returns NULL and does not read the file.
This is my function:
int file_parse() {
char filename[100];
printf("Enter the filename: ");
scanf("%s", filename); //Get filename
printf("\nYou want to encrypt or decrypt with file: %s", filename); //Confirm filename
int origin[27];
int final[27];
FILE *fp;
if ((fp = fopen(filename, "r")) != NULL) {
char current_line[250];
while (!feof(fp)) {
fgets(current_line, 250, fp);
printf("The current line is: %s", current_line);
}
fclose(fp);
} else {
printf("\nError! Cannot open the specified file.");
exit(EXIT_FAILURE);
}
}

How can I write or append the content fo text file then display?

How can I write and create the file, append if the file exists, then display all string file text? I can't append the content to at the end of file text, then display all strings. Thank for reading!
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<unistd.h>
int main(int argc, char** argv) {
char c, filename[100], content[100];
FILE *fptr;
printf("File name: ");
scanf("%s", filename);
printf("Enter content: ");
gets(content);
if ((fptr = fopen(filename, "r")) == NULL)
{
fptr = fopen(fptr, "w");
fprintf(fptr,"%s", content);
}
else{
fptr = fopen(fptr, "a");
fprintf(fptr,"%s", content);
}
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}
fclose(fptr);
return 0;
}
If you want to open a file for reading and to also append to it, you can do that with just one call to fopen by using the mode a+.
fptr = fopen(filename, "a+");
if (fptr == NULL)
{
// Handle not being able to open the file
}
If the file doesn't exist, it will create it. The position for reading will be at the beginning of the file, but anything you write to it will be at the end.
There are lot of bugs as mentioned by others in comments. I tried to explain in comments, read it carefully.
int main(int argc, char** argv) {
char filename[100], content[100];
FILE *fptr;
printf("Enter content: \n");
fgets(content,sizeof(content),stdin); /*use fgets() instead of gets()*/
printf("File name: ");
scanf("%s", filename);
if ((fptr = fopen(filename, "r")) == NULL) {/*if doesn't exist */
fptr = fopen(filename, "w"); /*create it */
fprintf(fptr,"%s", content); /* and write it */
}
else{
/* it should be a+ if you want to read, as you are doing using fgetc() */
fptr = fopen(filename, "a+");/*if exist, write at end of file */
fprintf(fptr,"%s", content);/* write at end */
}
rewind(fptr);/* move the fptr to beginning to read further */
int c = 0; /* fgetc() returns integer */
while( (c = fgetc(fptr))!= EOF) {
printf ("%c\n", c);
}
fclose(fptr);
return 0;
}
Use fgets() instead of gets(). Read here Why is the gets function so dangerous that it should not be used?

reading a string from a file

I have one text file. I have to read one string from the text file. I am using c code. can any body help ?
Use fgets to read string from files in C.
Something like:
#include <stdio.h>
#define BUZZ_SIZE 1024
int main(int argc, char **argv)
{
char buff[BUZZ_SIZE];
FILE *f = fopen("f.txt", "r");
fgets(buff, BUZZ_SIZE, f);
printf("String read: %s\n", buff);
fclose(f);
return 0;
}
Security checks avoided for simplicity.
This should work, it will read a whole line (it's not quite clear what you mean by "string"):
#include <stdio.h>
#include <stdlib.h>
int read_line(FILE *in, char *buffer, size_t max)
{
return fgets(buffer, max, in) == buffer;
}
int main(void)
{
FILE *in;
if((in = fopen("foo.txt", "rt")) != NULL)
{
char line[256];
if(read_line(in, line, sizeof line))
printf("read '%s' OK", line);
else
printf("read error\n");
fclose(in);
}
return EXIT_SUCCESS;
}
The return value is 1 if all went well, 0 on error.
Since this uses a plain fgets(), it will retain the '\n' line feed at the end of the line (if present).
void read_file(char string[60])
{
FILE *fp;
char filename[20];
printf("File to open: \n", &filename );
gets(filename);
fp = fopen(filename, "r"); /* open file for input */
if (fp) /* If no error occurred while opening file */
{ /* input the data from the file. */
fgets(string, 60, fp); /* read the name from the file */
string[strlen(string)] = '\0';
printf("The name read from the file is %s.\n", string );
}
else /* If error occurred, display message. */
{
printf("An error occurred while opening the file.\n");
}
fclose(fp); /* close the input file */
}
This is a Simple way to get the string from file.
#include<stdio.h>
#include<stdlib.h>
#define SIZE 2048
int main(){
char read_el[SIZE];
FILE *fp=fopen("Sample.txt", "r");
if(fp == NULL){
printf("File Opening Error!!");
}
while (fgets(read_el, SIZE, fp) != NULL)
printf(" %s ", read_el);
fclose(fp);
return 0;
}

Resources