how to read .docx file in c [closed] - c

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.

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);

recursive search for a file with a certain extension...C language [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 2 years ago.
Improve this question
Can anyone suggest me how to do a recursive search for a file with a certain extension using system calls?I should use opendir() but I don't quite understand how it works and how to use it recursively.
I should use opendir() but I don't quite understand how it works and how to use it recursively.
Idiomatically, opendir() is not used recursively, rather it is used in conjunction with readdir() in a loop, iteratively to list files, which can include other directories.
An example of using opendir(), and readdir() in a loop to find files. (from here)
This example is directly from the link with the exception of the strstr() call, used to pull out the files with the extension you are looking for. (See comment in code):
#include <stdio.h>
#include <dirent.h>
int main()
{
DIR *folder;
struct dirent *entry;
int files = 0;
folder = opendir(".");
if(folder == NULL)
{
perror("Unable to read directory");
return(1);
}
while(entry=readdir(folder))
{
files++;
if(strstr(entry->d_name, ".csv"))//Added to illustrate
//(change csv to extension you are looking for)
{
printf("File %3d: %s\n",
files,
entry->d_name);
}
}
closedir(folder);
return(0);
}
If you did indeed mean recursively, there are some ideas how to do that here.

file opening and writing in c [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 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).

C open file using a function [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
I am pretty new to C and I am struggling to write to files form my C program. In this program I open files to read from as well as files to write to, so I thought I'll make a function that opens the file for me. This is what I did
FILE *open_file(char* filename, char* mode)
{
FILE *f;
f = fopen(filename, mode);
if(!f)
{
perror(filename)
exit(EXIT_FAILLURE);
}
return f;
}
And this is how I call the function:
FILE *infile = open_file(args.infile_name, "r");
FILE *rm_file = open_file(args.rm_file, "r");
FILE *ex_file = open_file(args.ex_file, "w");
Those function calls are spread throughout the program. first I had the mode "r" hardcoded in there because I only needed to read from files, but now I also want to write for a file, so I made a parameter for the modus in which the file should be opened. It worked fine before, but now it does nothing. I compiles fine, without any errors, warnings and/or notes. Also valgrind tells me that everything is fine. Any thoughts on this? Thanks in advance.
EDIT: I forgot to add the line where I returned f, I added it now. It was in my code already (ofcourse, you can't let a non void function return nothing).
EDIT: Better explanation of the "error": I am trying to read words from a file and store them in a tree like datastructure. This is working fine, I can look them up and all so no problems here. After adding words to the tree I print the amount of words found in the file and the amount of words added to the tree (I don't add words that are already there). Now I want to export the tree to a file so I need to open a file I can write to. Since I had hardcoded the open_file with the read-only modus, I added a parameter for the modus. But when I run the program now it gives no error (so the filepointer isn't NULL) but just says 0 words found in file and 0 words added. Also the file I print to stays empty. Hope this makes it a little clearer
FILE *open_file(char* filename, char* mode)
{
FILE *f;
f = fopen(filename, mode);
if(!f)
{
perror(filename)
exit(EXIT_FAILLURE);
}
}
Your function doesn't return anything. Simply add return f; at the end. Seems your wrapper is doing little work though, could have also put this code where you are using it directly, instead of having this function.

Resources