How to read from a file using fgets? - c

I'm trying to read the contents of "Danfilez.txt" using fgets. However on completion the program returns a random value and i'm unsure why. I'm new to programming so any help would be greatly appreciated!
int main()
{
FILE* Danfile = fopen ("Danfilez.txt", "w");
char fileinfo [50];// Character arrays for file data //
if (Danfile == NULL)
{
printf ("ERROR\n");
}
else
{
printf("Everything works!\n");
fprintf (Danfile, "Welcome to Dan's file.");
fgets(fileinfo,50,Danfile);
printf("%s\n",fileinfo);
fclose (Danfile); // CLOSES FILE //
}
return 0;
}

Since you're both reading and writing from the file you want to use "w+" to open the file rather than just "w".
But that won't fix things because once you've written out that text, your position in the file is still at the end, so you'll also need to reset the position before you can read anything in using fseek()
fseek(Danfile,0,SEEK_SET);

While using fopen() you pass the option for opening as an argument to the funtion. Here is the list:
"r" - Opens the file for reading. The file must exist.
"w" - Creates an empty file for writing. If a file with the same name already exists,
its content is erased and the file is considered as a new empty file.
"a" - Appends to a file. Writing operations, append data at the end of the
file. The file is created if it does not exist.
"r+" - Opens a file to update both reading and writing. The file must exist.
"w+" - Creates an empty file for both reading and writing.
"a+" - Opens a file for reading and appending.
Try using "r+" or "w+". After writing some text, your position in the file will move forward along with the text. Use rewind(FILE* filename) to move your position straight to the start of the file. For more information related to file handling i recommend checking what is inside stdio library:
https://www.tutorialspoint.com/c_standard_library/stdio_h.htm

Related

Removing and renaming files with C error

So i'm trying updating data from original file to a new temporary file, then i remove the original file and rename the temp file with the name of the original one like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(){
FILE * fptr = NULL;
FILE * temp;
fptr = fopen("Original.txt", "rw+");
temp = fopen("temp.txt", "w");
if(fptr==NULL)//Check if file was opened successfully
{
printf("File could not be opened");
}
//cicle of me adding data to temp file
fclose(fptr);
fclose(temp);
remove("Original.txt");
rename("temp.txt","Original.txt");
return 0;
}
it works the first time, but when i execute again the Original.txt ends up blank. What can be causing that?
I've tried changing temp = fopen("temp.txt", "w"); to temp = fopen("temp.txt", "ab+"); but the info of Original.txt ends up with no spaces or new lines between (all together). And same with "wb"
"rb" the Original.txt file disappears.
if necessary ill add the entire code, althought i dont think its revelant
The moment you do an fopen (path, "w") it truncates the file pointed by the path. Then you close the file. Therefore essentially, your "temp.txt" is an empty file after the line fclose (temp). Get rid of the "w+" and "w" as they will truncate the file (make it zero length).
See man fopen
w Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.
w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned
at the beginning of the file.
You do not need to open and or close the file at all. If you see the remove and rename uses absolutely nothing from the previous part of the code. Also check man rename and man remove. If you want to know if the files are present of not. Check the return values of these functions. In case of error, you can always inspect the errno to understand what exactly went wrong. (See manpage for details).
Not sure what you are trying to do exactly, but depending on your question, you need only "r" mode for reading.
i.e, fptr = fopen("Original.txt", "r");
Additionally, couple of suggestions, but not answer to your question:
You are checking fptr == NULL, but proceeding to write data anyway? what if fptr == NULL is true and you try to read from a NULL pointer? Besides putting an error message, put a return -1 or something so that the program does not continue further for that case.
You should check temp == NULL as well, to detect whether file is opened for writing also. What if temp == NULL is true and you try to write to a NULL pointer?

File Writing using fopen_s() & fseek() with SEEK_END

Situation
I'm trying to programatically write some code to a file to then be compiled. I need to be able to add code in without replacing the whole file. I have a } on the end of the file, which I need to remove to be able to add the next lot of code, after which the } is readded.
My code is currently thus:
//Writes from an offset from the end of a file.
static bool Write(const char *FilePath, const char *Text)
{
FILE *f;
errno_t error;
int seekerror;
if (!(error = fopen_s(&f, FilePath, "w")))
{
//one byte offset from the end of the file.
if (!(seekerror = fseek(f, -1, SEEK_END)))
{
fwrite(Text, sizeof(char), strlen(Text), f);
fclose(f);
return true;
}
}
return false;
}
Issue
I believe the w and a flags override the pointer location from fseek()? So I don't seem to be able to overwrite the } at the end of the file.
Does anyone know how I'd be able to write from an offset using SEEK_END?
Don't use "w" mode for fopen, use "r+" instead.
"r" - Opens a file for reading. The file must exist.
"w" - Creates an empty file for writing. If a file with the same name already
exists, its content is erased and the file is considered as a new empty file.
"a" - Appends to a file. Writing operations, append data at the end of the
file. The file is created if it does not exist.
"r+" - Opens a file to update both reading and writing. The file must exist.
"w+" - Creates an empty file for both reading and writing.
"a+" - Opens a file for reading and appending.

Reading a File in C: different behavior for "r" and "a+" flags

I want to open a file, read its contents, and then append a line to the file. I thought I should use the "a+" flag for the task.
I have a function which opens a file and returns a pointer to this file.
FILE* open_weekly_disk_file(char* filename){
FILE* weekly_log;
weekly_log = fopen(filename, "a+");
//weekly_log = fopen(filename, "r");
if(! weekly_log){
printf("The attempt to open the weekly log failed!\n");
return NULL;
} else{
return weekly_log;
}
}
Then I have a function which calls the function above and uses scanf to read contents from the file:
void sample_function(char* filename){
FILE* log;
char token[100], current_read[100];
int limit;
log = opened_weekly_disk_file(filename);
// The problem happens here
for(limit=0; limit < TOKEN_NUMBER; limit++){
if(fscanf(log, "%s%s", &token, &current_read) == 2){
printf("%s %s\n", token, current_read);
}
}
...
}
This code works when I use:
weekly_log = fopen(filename, "r");
But does not work when I change the "r" flag to "a+". I get a Segmentation fault right before the for loop.
That is because the mode spec "a" opens a file for appending, with the file pointer at the end. If you try to read from here, there is no data since the file pointer is at EOF. You should open with "r+" for reading and writing. If you read the whole file before writing, then the file pointer will be correctly positioned to append when you write more data.
If this is not enough, please explore ftell() and fseek() functions.
from this SO QA
from the man page:
a+
Open for reading and appending (writing at end of file). The file is
created if it does not exist. The initial file position for reading is
at the beginning of the file, but output is always appended to the end
of the file.
Answer:
There is just one pointer which initially is at the start of the file
but when a write operation is attempted it is moved to the end of the
file. You can reposition it using fseek or rewind anywhere in the file
for reading, but writing operations will move it back to the end of
file.
So, the problem is not the fact that the file is opened in append mode, because it is not, as far as reading from it is concerned.
The problem lies in what your code does in those three dots
log = opened_weekly_disk_file(filename);
...
The code quite probably writes to the file, making the file cursor move to the end of it before the reading occurs.

Having problems with fseek() in C

So, I have this function on my program that is supposed to save a "car_str" structure into the desired place on a file specified as a parameter. But when I run it, it keeps overwriting the first slot again and again, as if fseek didn't point to the specified place on the file. Is there any problem with my code? I think it may be related with the multiplication, since without it the program does well, but I cannot point to the place I want.
void save(int car_nbr)
{
FILE *f;
f = fopen("memory.txt","wb");
if (!f)
{
printf ("error");
}
else
{
car_nbr--;
fseek(f, sizeof(struct car_str)*car_nbr, SEEK_SET);
fwrite(&car,sizeof(struct car_str),1,f);
rewind(f);
fclose(f);
printf("\nsaved");
}
}
you need to fopen with r+b.
if you fail than file not exist, so you can try use "wb"
"w" - write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.
"r+" - read/update: Open a file for update (both for input and output). The file must exist.
f = fopen("memory.txt","r+b");

fscanf doesn't read anything

i am trying to read integers from a file and fscanf doesn't work well with this code.
fp=fopen("record.dat","r");
if(fp==NULL)
{
printf("Another reading error");
}
else
{
printf("\nstarting to read\n");
i=0;
while(i<10)
{
if(fscanf(fp,"%d",&temp)>0)
printf("%d\n",temp);
i++;
}
fclose(fp);
}
the file contains 10 numbers which are delimited by a new line character. This code doesn't produce or print anything. What is the problem with the code and pls help me with it.
EDIT
the access mode as w+ or r isn't giving me a correct expected answer.
You are opening the file as a writable file instead of readable.
You must change "w+" to "r"
w+ The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
"w+" actually opens file for reading and writing. However, file is truncated to 0 length.
That might be the cause of printing blank lines.
Try "r+" (opens the file for reading and writing, without truncation) or "r".

Resources