the function fgetc is not working properly - c

i'm testing the fgetc() function but it doesn't work properly (i have used this function befor so i know how it works)
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *file = NULL;
int n;
file = fopen("test.txt", "w+");
if(file != NULL)
{
fputs("ab", file);
printf("%c", fgetc(file));
}
else
{
printf("error");
}
return 0;
}
the output should be "a" but it's somthing else

The file is opened for both writing and reading but you need to fseek to the correct place in the file (here, the beginning). In particular, when switching between writing and reading you need to fseek or fflush.
When the "r+", "w+", or "a+" access type is specified, both reading
and writing are enabled (the file is said to be open for "update").
However, when you switch from reading to writing, the input operation
must encounter an EOF marker. If there is no EOF, you must use an
intervening call to a file positioning function. The file positioning
functions are fsetpos, fseek, and rewind. When you switch from writing
to reading, you must use an intervening call to either fflush or to a
file positioning function.
In any case, after writing to the file, the file pointer is in the wrong place to read what was just written.
So the code becomes
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *file = NULL;
file = fopen("test.txt", "w+");
if(file != NULL) {
fputs("ab", file);
fseek(file, 0, SEEK_SET);
printf("%c", fgetc(file));
fclose(file);
}
else {
printf("error");
}
return 0;
}
And if you want to continue writing to the file, you must fseek to its end.

Your error is that you are trying to read a file that has been opened for writting. You should write inside it, then close the file and reopen it for reading. This code will show what I am telling:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fileRead, *fileWrite = NULL;
int n;
fileWrite = fopen("test.txt", "w+");
if(fileWrite != NULL)
{
fputs("ab", fileWrite);
fclose(fileWrite);
}
else
{
printf("error");
}
// Open again the file for read
fileRead = fopen("test.txt", "r");
printf("%c", fgetc(fileRead));
fclose(fileWrite);
// End function
return 0;
}

Related

Is opening and closing the same file multiple times a bad practice in C?

my C program does 3 things:
reads from a text file
converts all letters into upper-case
prints the converted text into the console.
Here are how many times I'm opening and closing those 2 files in my program:
Original => 1 for "r"
New => 2 first for "w+" and last for "r"
Is there a better way to write to a file and read from it without opening and closing a multiple times? (even though I only opened and closed twice, I wanna build a good practice)
#include <stdio.h>
#include <ctype.h>
int main()
{
const char ORIGINAL[] = "challenge2.txt";
FILE *fp = NULL;
FILE *fpNew = NULL;
char ch, ch2;
///////////// open the original txt file to read /////////////
fp = fopen(ORIGINAL, "r");
if (fp == NULL)
{
perror("Error opening the file");
return (-1);
}
///////////// create and write on a new file /////////////
fpNew = fopen("challenge2_copy.txt", "w+");
printf("\n============== Original text ==============\n");
while ((ch = fgetc(fp)) != EOF)
{
printf("%c", ch);
ch = toupper(ch);
fputc(ch, fpNew);
}
fclose(fp);
fp = NULL;
fclose(fpNew);
fpNew = NULL;
///////////// call the new file to print the converted text /////////////
fpNew = fopen("challenge2_copy.txt", "r");
if (fpNew == NULL)
{
perror("Error opening the file");
return (-1);
}
printf("\n============== Converted to Uppercase ==============\n");
while ((ch2 = fgetc(fpNew)) != EOF)
{
printf("%c", ch2);
}
fclose(fpNew);
fpNew = NULL;
return 0;
}
Here's the console output:
============== Original text ==============
hello I AM JACK
I AM TESTING lowerCASE
GONNA convert THIS into
UPPERcase
i hope THIS works
============== Converted to Uppercase ==============
HELLO I AM JACK
I AM TESTING LOWERCASE
GONNA CONVERT THIS INTO
UPPERCASE
I HOPE THIS WORKS
Good practices, performance, dangers.
MS Visual Studio suggest using fopen_s as good practise :)
Sometimes reopening file sometimes makes code more clear to read in big projects.
As for performance, it will take some time for processor to make new FILE instance and fill it with all file properties.
There can also be some interrupts eg. after releasing ownership for a while cloud sync. tools may want to back up newly created file and will block accessing it for other apps. (your program).
Performance solution.
So as to reuse a FILE instance you need only to jump into different place in FILE buffer (eg. start of the file).
You can achieve it with fsetpos or fseek functions from stdio.h.
https://www.cplusplus.com/reference/cstdio/fsetpos/
https://www.cplusplus.com/reference/cstdio/fseek/
Example FILE instance reusage.
/* fsetpos example */
#include <stdio.h>
int main ()
{
FILE * pFile; fpos_t position;
#define buffSize 1024 //1KB
char s[buffSize];
//Write
pFile = fopen ("myfile.txt","w+");
fgetpos (pFile, &position);
fputs ("That is a sample",pFile);
//Reuse for reading
fsetpos (pFile, &position);
puts (fgets(s,buffSize, pFile));
//Next reuse for reading
fsetpos (pFile, &position);
puts (fgets(s,buffSize, pFile));
fclose (pFile);
return 0;
}
The above code produces the following result:
That is a sample
That is a sample

trying read and write on the same code in c program

Help I'm trying to write the data in the file. then, trying to read it back, but its not working.
#include <stdio.h>
int main(void) {
FILE *fptr = fopen("try.txt", "r+");
char line[1000];
fprintf(fptr, "i have new number = 1425");
while (fgets(line, 1000, fptr)) {
printf("%s",line);
}
return 0;
}
You must use a positioning function such as rewind() or fseek() between read and write operations.
Beware that the update mode for streams is very confusing and error prone, you should avoid using it and structure your programs accordingly.
Incidentally, your program will fail to open try.txt if it does not already exist, but you do not check for fopen failure so you will get undefined behavior in this case.
Here is a modified version:
#include <stdio.h>
int main(void) {
char line[1000];
FILE *fptr = fopen("try.txt", "w+");
if (fptr != NULL) {
fprintf(fptr, "I have new number = 1425\n");
rewind(fptr);
while (fgets(line, sizeof line, fptr)) {
printf("%s", line);
}
fclose(fptr);
}
return 0;
}

modify first number from a read file c language

int main()
{
FILE *file=fopen("numbers.dat","rb");
int number;
if(file ==NULL)
exit(0);
while(fread(&number,sizeof(int),1,file))
printf("%d",number);
return 0 ;
}
The code below opens a file in read/write bytes mode and writes a number to the beginning of the file without erasing its content, is that what you need ?
#include <stdio.h>
#include <stdlib.h>
int main () {
FILE * fp;
int a = 12;
fp = fopen ("titi", "rb+");
fwrite(&a, sizeof(int), 1, fp);
fclose(fp);
return(0);
}
If you want to read it first, you might want to look at the fseek() function to place your file cursor back at the beginning of the file or open the file twice, once to read it and once to write to it.

C language: reading a .txt file

I'm trying to write a program that reads a text file, using C with Visual Studio.
This is my current code (which doesn't work):
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *filePTR;
char fileRow[100];
filePTR = fopen_s(&filePTR, "text.txt", "r");
// Opens the file
if(filePTR){
while(!feof(filePTR)) {
// Reads file row
fgets(fileRow, 100, filePTR);
// Displays file row
printf("%s \n", fileRow);
}
printf("\nEnd of file.");
}
else {
printf("ERROR! Impossible to read the file.");
}
// Closes the file
fclose(filePTR);
return 0;
}
I'm getting the following warning:
'filePTR' may be '0': this condition does not meet the function specification 'fclose'.
What am I doing wrong? I haven't been programming in C since a while ...
The problems begin long before the fclose. This line is incorrect:
filePTR = fopen_s(&filePTR, "text.txt", "r");
It overwites the file pointer already assigned by passing a pointer as the function argument &filePTR.
The function returns an error status, not the file pointer. Please see the man page:
Return Value Zero if successful; an error code on failure.
Also, please see Why is while ( !feof (file) ) always wrong?
I suggest this:
#include <stdio.h>
#include <stdlib.h>
int main(void) { // correct definition
FILE *filePTR;
char fileRow[100];
if(fopen_s(&filePTR, "text.txt", "r") == 0) {
while(fgets(fileRow, sizeof fileRow, filePTR) != NULL) {
printf("%s", fileRow); // the string already contains a newline
}
fclose(filePTR); // only close if it was opened
printf("\nEnd of file.");
}
else {
printf("ERROR! Impossible to read the file.");
}
return 0;
}
Note that I moved the fclose call up. You can't close a file that you did not open.

fwrite() appends instead of write C

I have to write a program witch reads from a file received by line and then it overwrites it with the read words uppercased.
This is my code
void toUpperCase(char* string) {
int i=0;
while(string[i])
{
string[i]=toupper(string[i]);
i++;
} }
int main(int argc, char** argv) {
if(argc==1)
{
puts("Error: INSERT PATH");
exit(0);
}
char* file=argv[1];
FILE* fd=fopen(file,"r+");
if(fd<0)
{
perror("Error opening file: ");
exit(0);
}
char buffer[30][30];
int i=0;
while(!feof(fd))
{
fscanf(fd,"%s",buffer[i]);
i++;
}
int j=0;
for(j=0; j<i; j++)
{
toUpperCase(buffer[j]);
fwrite(buffer[j],strlen(buffer[j]),1,fd);
}
fclose(fd);
return 0; }
but this program appends the words contained in buffer[][] instead of overwriting the file.
If the file contain was something like pippo pluto foo then, after the execution is pippo pluto fooPIPPOPLUTOFOO instead of PIPPO PLUTO FOO.
Where am i wrong? Thank you
You have to reset the file position indicator using fseek, as fscanf will advance it. Something like
fseek(fd, length_of_read_string, SEEK_CUR);
This allows you to read the file in chunks, but it will be tricky to get right. Or of course reset it to the file start because you read everything in 1 go:
fseek(fd, 0L, SEEK_SET);
I strongly recommend writing the modified data into a new file, and then after the program has run, delete the initial file and rename the new one. That will also take care of another issue with your program, you are reading the entire file into memory before handling it.
If you want to do in-place translation that doesn't change lengths, you can open the source file in two streams and then do read-chunk, write-chunk in lockstep. That has the advantage of being super-easy to convert to a non-in-place version that will work with nonseekable files too (stdin/stdout, pipes, and sockets).
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h> //toupper
inline void upcaseStr(char* str){
for(;*str;str++) { *str=toupper(*str); }
}
int upcaseStream(FILE* in, FILE* out){
char buf[BUFSIZ]; //BUFSIZ is an implementation-defined constant for an optimal buffer size
while(fgets(buf, BUFSIZ, in)){
upcaseStr(buf);
if(fputs(buf, out) == EOF){ return 1; }
}
if(!feof){ return 1; }
return 0;
}
int main(int argc, char **argv)
{
//default in and out
FILE* in = stdin;
FILE* out = stdout;
if(argc == 2) {
in = fopen(argv[1], "r"); //for reading
out = fopen(argv[1], "r+"); //for writing (and reading) starting at the beginning
if(!(in && out)){
fprintf(stderr, "Error opening file %s for reading and writing: %s\n", argv[1], strerror(errno));
}
}
return upcaseStream(in, out);
}
If you do use the in-place version, then in the unlikely event that the if(fputs(buf, out) == EOF){ return 1; } line should return, you're screwed unless you have a backup copy of the file. :)
Note:
You shouldn't name your FILE pointers fd because C people will tend to think you mean "file descriptor". FILE is a struct around a file descriptor. A file descriptor is just an int that you can use for FILE access with the raw system calls. FILE streams are an abstraction layer on top of file descriptors--they aren't file descriptors.
As you read from the file, its internal position indicator gets moved. Once you start writing, you start writing from that position on, which happens to be at the end of the file. So you effectively append the data to the file.
Rewind the handle to reset the position indicator before writing into the file:
rewind(fp);
On a side note, you are reading the file incorrectly:
while(!feof(fd))
{
fscanf(fd,"%s",buffer[i]);
i++;
}
When you reach the end of the file, fscanf will return an error and not read anything, yet you still increment variable i, as if the read was successful. And then you check feof() for end-of-file, but i was already incremented.
Check feof() and return of fscanf() immediately after calling fscanf():
while(1)
{
int read = fscanf(fd,"%s",buffer[i]);
if( read != 1 )
//handle invalid read
if( feof(fd) )
break;
i++;
}
Think about what happens if the string is longer than 29 characters and/or the file contains more than 30 strings. char buffer[30][30];
Welcome to StackOverflow!
Reopening the stream with fopen with the "w" parameter:
fd=fopen(file, "w");
It opens the file and if there are any contents in the file, it clears them.

Resources