rewind and fseek don't work - C [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
int main()
{
FILE *file1, *file2;
char filename[] = "test.xml";
char c;
int line = 1;
//open file in read mode
file1 = fopen(filename, "r");
c = getc(file1);
while (c != EOF){
printf("%c", c);
c = getc(file1);
}
//rewind
rewind(file1);
//fseek(file1, 0, SEEK_SET);
//open new file in write mode
file2 = fopen("replica.c", "w");
c = getc(file1);
if(c == EOF) printf("toto");
}
The rewind() and fseek() functions don't work, my program display "toto", so file1 is still positioned on EOF.
Do you have an idea to solve this probleme please?

If all your program prints is toto, then this can be explained by the file test.xml being empty.

Related

Writing to a file. Creates file but does not write in it [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am writing a program that reads a string and writes that string in another file which has not been created.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char s[50];
FILE *fp;
fp = fopen("E:\\poem.txt","w");
if(fp = NULL)
{
printf("Cannot open file");
exit(1);
}
printf("Enter a string\n");
while(strlen(gets(s))>0)
{
fputs(s,fp);
fputs("\n",fp);
}
fclose(fp);
return 0;
}
Since the "w" mode creates a new file if file is not already created,my program creates that file however it is unable to write it to the file
The double slash in fp = fopen("E:\\poem.txt","w"); is because i thought \p cannot be a escape sequence but i want to go to the directory E:\ so i used double slash.
However I even tried fp = fopen("poem.txt","w"); same thing happen creates a file but doesnot write on it.
Also checked this question but was not helpful C: can't write data on file
From man page of gets():
gets() returns s on success, and NULL on error or when end of file
occurs while no characters have been read.
When gets() return NULL (on failure), then strlen(NULL) causes segmentation fault.
So, you can simply use while(gets(s)!=NULL) instead of while(strlen(gets(s)) > 0)
As you mentioned in comment a typo use== instead of =
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char s[50];
FILE *fp;
fp = fopen("E:\\poem.txt", "w");
if (fp == NULL)
{
printf("Cannot open file");
exit(1);
}
printf("Enter a string\n");
while (strlen(gets(s)) > 0)
{
fputs(s, fp);
fputs("\n", fp);
}
fclose(fp);
return 0;
}

Can't read file in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to read file with C program here is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * fptr;
fptr = fopen("text.txt","r");
char arr[150];
char c;
int i;
while(!feof(fptr) && i<5)
{
printf("%d\n",i++);
fgets(arr,150,fptr);
puts(arr);
}
fclose(fptr);
return 0;
}
When executed the program wont stop and the characters printed are weird, i dont know what is going wrong ?
The part causing error in your program is :
while(!feof(fptr))
Better read : What is wrong with "while(!feof(fptr))" and Why it's bad to use feof() to control a loop
A simple program to read is below which checks if file is opened or not. It's a good practice to check if file you are to perform operations on is opened or not.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
FILE *fp;
fp = fopen("text.txt", "r"); // read mode
if (fp == NULL) //Checking if file is open
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are:\n", file_name);
while((ch = fgetc(fp)) != EOF){
printf("%c", ch); //Avoided creating a buffer
}
fclose(fp);
return 0;
}
#Though not so much realevant!!
I think the easiest way to read/write from/to file is using freopen() function. You can use scanf() & printf() in case of C and cin & cout in case C++ to read or write from file with this function.
Read from file: freopen("input.txt","r",stdin); where input.txt is filename.
Write to file: freopen("output.txt","w",stdout); no need to create output.txt your own. The file is automatically created when the program is executed.

Cannot open Files and getting error code 0x80070002 on C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hi i had a sample code for reading and writing on a txt file but visual studio wont read the code and keep crashing and giving same error 0x80070002 (error happens in bot "w" and "r" formats)
here is the code
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE* fp;
if ((fp = fopen("C:\Users\39351\source\repos\exercise\prova.txt", "r")) == NULL)
exit(1);
else {
int c;
while ((c = fgetc(fp)) != EOF) putchar(c);
fclose(fp);
}
}
no matter the code when i use the fopen command even fopen_s it gives this error
can someone help?
The backslash (\) character introduces an escape sequence in C string literals, so it's trying to process the \U, \3, \s, \r, \e, and \p as escape sequences. For example, \r translates to a carriage return character. To fix this you need to double up the backslash characters:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE* fp;
if ((fp = fopen("C:\\Users\\39351\\source\\repos\\exercise\\prova.txt", "r")) == NULL)
exit(1);
else {
int c;
while ((c = fgetc(fp)) != EOF) putchar(c);
fclose(fp);
}
}
try to use '/' istead '\'
you can change the location of the file, it may be a permission failure

Copying file text in C using getc() and putc() - binary code in the output file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I created a file called "text.txt" with a string inside and I want to copy that string in another file called "copiaqui.txt". But there's a problem. In the output file, I found this :
Why the program doesn't copy the string correctly?
Code :
#include <stdio.h>
#include <stdlib.h>
void copiaFile(FILE *fi, FILE *fo);
int main(void)
{
FILE *fi = fopen("test.txt", "r");
FILE *fo = fopen("copiaqui.txt","w");
if (fi == NULL)
{
printf("\nImpossibile aprire il file test.txt\n");
exit(EXIT_FAILURE);
}
if (fo == NULL)
{
printf("\nImpossibile aprire il file copiaqui.txt\n");
exit(EXIT_FAILURE);
}
copiaFile(fi, fo);
fclose(fi);
fclose(fo);
return 0;
}
void copiaFile(FILE *fi, FILE *fo)
{
int var;
while((var = getc(fi) != EOF))
{
printf("\nCarattere acquisisto : %c", var);
putc(var, fo);
}
}
You have made a common mistake with this expression:
var = getc(fi) != EOF
What this does is assign the value of (getc(fi) != EOF) to var, because of something called operator precedence. The value is either true or false. What you intended to do is:
(var = getc(fi)) != EOF
Which will make var have the getc() value, then check that against EOF.

How to dynamically change filename while writing in a loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to open file in 20 loop. Every time the name of the folder changes.Like This variables1,variables2,variables3......variables20 I found the same question in here , but it didnt help me.
Here's what I have tried:
int l=1;
while(l<20){
char filename[10];
sprintf (filename, "variables%d", l);
OR
scanf("%s", filename);
FILE * fp;
if ((fp = fopen (filename,"rb")) == NULL){
printf("Failed to Open File variables%d\n",l);}
........... Reading Data........
fclose (fp);
l++;
}
I can wite Filename succesfully but I got the error: Failed to Open File variables1
[SOLVED] I am just sodding idiot.Thank you for your concern and answers... i just forgot to add ".bin" sprintf (filename, "variables%d.bin", l);
You never increment your counter.
I would also recommend you to use a for loop like this
for(int i = 1; i < 20; i++){
// Your code
}
Your filename buffer is too short - "variables1" requires 10 characters plus a '\0' terminator, so you need at least 11 characters for this buffer, and more when the index is > 9, otherwise you will get a buffer overflow and undefined behaviour. Change:
char filename[10];
to:
char filename[PATH_MAX]; // PATH_MAX is defined in <limits.h>
Also: if, as your title suggests, you want to write to these files, then you need to change:
if ((fp = fopen (filename,"rb")) == NULL){
to:
if ((fp = fopen (filename,"wb")) == NULL){

Resources