Cannot open Files and getting error code 0x80070002 on 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 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

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.

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.

rewind and fseek don't work - 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 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.

Junk values from an input file in C [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
So I have this bit of C code below. When I place printf statements to test the text from the input file, I see that I'm getting a bunch of junk values, to be more specific they are not even alphabetic or numerical, I think they are diamonds with question marks in them. I assume this means it is not processing these values the way it should be. The input file a bit of MIPS assembly code, but in this context it is only a text file. I have commented out all other parts of my program and am left with this small piece and yet I still receive the bad values. What could I possibly be doing wrong here?
The command I use to run the program on the console is:
./assembler -symbols adder.asm
Where ./assembler is the driver (argv[0])
-symbols is a tag used (argv[1])
adder.asm is the input file (argv[2])
So once opened I should be able to grab text out of this file, and it's not a problem with the file as far as I believe, it was working earlier.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
FILE *fp;
FILE *fp_out;
void main(int argc, char* argv[])
{
int mode;
if (strcmp(argv[1], "-symbols") == 0)
{
fp = fopen(argv[2], "r");
mode = 1;
}
else
{
fp = fopen(argv[1], "r");
fp_out = fopen(argv[2], "w");
mode = 2;
}
}
Try to add the following line right after the open section and add #include <errno.h> to the beginning.
printf("%p, %p, %d\n", fp, ftp_out, errno);
If the fp is null then there is some problem opening the file. If you do not check the return value, you can read from a wrong buffer. Maybe there is some permission problems (or whatever). Also if errno != 0 you have a problem. Check with perror <num> the errno value in command line (or see perror(3) function).

Resources