file opening and writing in c [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
FILE *fp1;
if((fp1 = fopen("abc.txt", "w")) != NULL)
{
fprintf(something);
fclose(fp1);
}
I am trying to compile this file and all related file using developer command prompt and trying to run it from there. But while running it says this filename.exe has stopped working. It is not able to create the file abc.txt .

You almost certainly don't have write permission in the directory you are attempting to create abc.txt in. The code would be better if you handled the error case:
if ((fp1 = fopen("abc.txt", "w")) == NULL) {
perror("abc.txt");
exit(1);
}
My guess is your output will be "abc.txt: Permission denied"
You also have a problem that your fprintf() ought to be fprintf(fp1, something) (where something is presumably your format string and some variables).

Related

Why doesn't fprintf(outputFile,"") work from inside gdb? [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 1 year ago.
Improve this question
When I try to debug a piece of code from inside gdb and the control reaches a fprintf statement to an outputFile it doesn't show the output lively in the file and it just shows empty, despite working fine with just a simple run of the program from the terminal.
How do I get my output lively on the outputFile when debugging from inside gdb?
NOTE: I am not talking about gdb precisely I am just asking how to get the output lively in the file on the fly while debugging from gdb.
How do I get my output lively on the outputFile when debugging from inside gdb?
Do this:
(gdb) call fflush(0)
This will cause the program being debugged to flush all of its output streams.
This is probably due to stream buffering:
Characters that are written to a stream are normally accumulated and transmitted asynchronously to the file in a block, instead of appearing as soon as they are output by the application program. Similarly, streams often retrieve input from the host environment in blocks rather than on a character-by-character basis. This is called buffering.
Try out this example:
#include <stdio.h>
int main(void) {
FILE *f = fopen("foo.txt", "w");
printf("Break here and go line by line.");
fprintf(f, "Bar\n");
printf("Nothing in foo.txt yet.");
fflush(f);
printf("Buffer has been written to foo.txt now.");
fprintf(f, "Baz\n");
printf("Baz is still only in the buffer.");
fclose(f);
printf("The buffer was written to the file before closing.");
}

fopen ordering in C affecting it strangely [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have this code:
FILE *setup=fopen(strcat(cwd,"setup.txt"),"r");//navigates up to setup file
...//(doing stuff with setup)
fclose(setup);
FILE *paths=fopen(strcat(cwd,"stuff.txt"),"r");
char buff2[1024];
fgets(buff2,200,paths);
char thing[1024];
strcpy(thing,buff2);
printf(thing);
fclose(paths);
but it executes differently based on if I do code with setup or the code with paths first - only the first one works properly, the second just creates gibberish whenever fgets is called. fclose returns 0 as it should. The specific way I look at the second file doesn't seem to affect it. What's happening here?
Don't strcat()!
FILE *setup=fopen(strcat(cwd,"setup.txt"),"r");//navigates up to setup file
// cwd now has "...setup.txt"
FILE *paths=fopen(strcat(cwd,"stuff.txt"),"r");
// cwd now has "...setup.txtstuff.txt"
strcat() modifies cwd.
So if cwd originally contains "/home/username/", the first call changes it to "/home/username/setup.txt".
The second call appends to that, so it tries to open "/home/username/setup.txtstuff.txt". This filename almost certainly doesn't exist, but you aren't checking for an error.
And if that filename is longer than the space allocated to cwd, you'll get a buffer overflow and undefined behavior.
Use a different variable to hold the filename to open.
char filename[MAXLENGTH];
sprintf(filename, "%s%s", cwd, "setup.txt");
FILE *setup = fopen(filename, "r");
...
fclose(setup);
sprintf(filename, "%s%s", cwd, "stuff.txt");
FILE *paths = fopen(filename, "r");
...
fclose(paths);

Getting a core segmentation fault when trying to read a file from argv[] [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 3 years ago.
Improve this question
I have been getting a core segmentation fault when trying to read a file from argv[]. I have created the file and I am very sure that I am trying to access the correct memory address. However, when trying to open the file I receive the core dump.
Here is the relavent code-
for (int x=1;x<argc;x++){
int buffsize=2056;
char buff[buffsize];
FILE* thefile;
thefile=fopen("argv[x]","r");
if (thefile == NULL) {
fprintf(stderr, "%s cannot open file '%s'\n", argv[0], argv[1]);
return 2;
}
The command line argument I am passing in is
./words testfile.txt
with ./words being the compiled code.
I actually found the solution, it seems to be the quotes in argv in the fopen, why is this?
Oops! I believe the line
thefile=fopen("argv[x]","r");
Should be:
thefile=fopen(argv[x],"r");
This is because "argv[x]" is not code, it's a string. What's that? Is that a path to a file? It's not compiled as code. It doesn't refer to one of the arguments in argv.
You might also want your log message to use that filename. Notice the argv[x]:
fprintf(stderr, "%s cannot open file '%s'\n", argv[0], argv[x]);

how to read .docx file in c [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I got abstract output which you can not read by running this
code.please tell me,how I can read the output or different method by
which I can read word file in c.I am a beginner and didn't found any detail information about this.
#include <stdio.h>
int main()
{
FILE *fr;
int c;
fr = fopen("Hello.docx", "r");
if(fr==NULL)
{
printf(" File NOT FOUND!");
}
while( c != EOF)
{
c= fgetc(fr); /* read from file*/
printf("%c",c); /* display on screen*/
}
fclose(fr);
return 0;
}
Reading a MS Word document with raw C programming is quite a big project, not suitable for beginners. It is not a pure text file so you can't use fopen("Hello.docx", "r");. Rather it is a custom format, so you'd have to open it as binary. Then read the 500+ pages long specification of the format and treat the data accordingly. Might be worth taking a peek at the Open Office code to get an idea of how much work that's involved.
You can't directly open a word file using fopen( ) as it is difficult to convert a .docx file. Reason being, it is a huge binary file and is compressed, thus while converting it might be possible you don't get the entire data.
Take a look at fseek( ) that might help.

"Parse" line of code doesn't allow code to execute [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Cheers, I've isolated the error but I'm not sure how to fix it. Apparently, this line of code,(C language):
parse(getenv("QUERY_STRING"));
It does successfully compile, however when I run the executable the following pops up: puu.sh/nQi41/40e81c4494.png
When I simply comment out that specific line, the code compiles and runes perfectly.
Any possible solutions to this? Thanks in advance
Replace:
parse(getenv("QUERY_STRING"));
by:
char *querystring = getenv("QUERY_STRING");
if (querystring == NULL)
{
printf("Could not get querystring");
exit(1);
}
parse(querystring);
... and read the documentation of getenv.

Resources