Searching in a .map file with C - c

i want to create a tool which is able to search for a specific word in a .map file.
The word i am searchin for is ".text". I have loaded the file with
input_file = fopen("file_location","r") in read mode.
Now i am trying to search in this file for the string .text. My file has over 200 lines.
I would really appreciate some help from your side to solve this problem, because if(input_file == ".text") is not working.
my next solution would be to check every line for a match, but i dont know how i can load a complete line of a file into an array ?
Thanks a lot

Use fgets() to read the whole line and see whether your word is there in that line using strstr() perform the same operation till you read the whole file.
char buf[100];
while(fgets(buf,sizeof(buf),input_file) != NULL)
{
if(strstr(buf,".text"))
printf("Word found in the file\n");
}

Related

How to check if line in xml file has specific tag and if found save value within tag in c

So I am quite new to c programming and was wondering whether there is a way to read through an xml file line by line and save the value between a tag if found.
So far I have created the following program.
#include <stdio.h>
int main () {
char line [150];
FILE *file = fopen("testfile.xml","r");
if (file == NULL) {
fprintf(stdout,"Unable to open file.\n");
exit(1);
}
while(fgets(ln, sizeof(ln), file)) {
fprintf(stdout,"%s\n",ln);
}
return (0);
}
This is an example of how my xml file looks. Multiple tags can be on the same line.
<Details>
<firstname>Steve</firstname>
<surname>Barrat</surname>
<age>20</age>
<info><addr>23 Boxwell road</addr><hobby>Football</hobby></info>
</Details>
Is there any method in which I can check if for example <addr> tag exists in the line read and if
so, save value from the content of that tag to a variable.
Any help would be great.
You don't want to read an XML file line-by-line because line breaks can occur anywhere, for example your file might be formatted as
<Details><firstname
>Steve</firstname><surname>Barrat</surname
><age>20</age><info><addr>23 Boxwell road</addr><hobby>Football</hobby></info>
</Details>
and you would still be expected to read it successfully. You should be reading it element-by-element, not line-by-line, and the tool you use to do so is an XML parser.
I'm afraid I'm not up to speed with the state of the art in XML parsing from C, beyond the fact that most people use libxml; more modern languages such as Java and C# are much better served.

Append Random Text to File (C)

I'm trying to create a file inside a directory, then append some random text inside of the file.
My Code
char dirname[30];
sprintf(dirname, "myroom.%d", (int)getpid());
mkdir(dirname,0777);
char path[path_max+1];
snprintf(path1, PATH_MAX+1, "%s/file1.txt,dirname);
FILE *filedir1 = fopen(path1, "a+");
fclose(filedir1);
char *random_name = { "burger", "toast", "burrito", "noodles" };
int number = rand();
fputs(random_name[number], filedir1];
What I want
(Inside directory "dirname")
When I open file1.txt, I expect there will be either the word burrito, burger, toast, or noodles in the first line.
What I get
file1.txt still empty.
Questions
Anybody know what happen with my code? I saw from youtube video, to append some text into a file, all I need is the fputs command but it doesn't seem to work in my code. Is it because I'm using "a+" in fopen?
Any help will be highly appreciated. Thanks
I think that rand() is creating problem.use rand()%4 instead.
And you are closing the file using fclose() before putting random word. Use it at last.
After opening a file, you are close that file. After closing the file, you are not able to enter the text to that file.
FILE *filedir1 = fopen(path1, "a+");
fclose(filedir1);
Your problem is in this fclose line. You have to do this in after finishing the write operation in that file.
So, remove the fclose(filedir1) and put this line after writing the file.

fopen using append on EXISTING files (malfunction)

Hi all I am relatively new to programming. Currently my code is working "correctly"
What I want to do is open a file for appending and then use couple different other source files to put data on the target file for appending.
The problem is that whenever I use fopen on the file name, it does not open the existing file but rather creates a file with no extension (all the files are in the same directory so should be able to open no problem). This is the code I am using to grab the necessary information for which file to append (error is never generated because it just creates a new file)
FILE *fp_target;
char target[100];
fgets(target, 99, stdin);
if ((fp_target = fopen(target, "a"))==NULL)
{
fprintf(stderr, "the following target file %s can not be appended", target);
exit (0);
}
Actually I tried this test a couple times and when i do this again with the same name used in the first try (lets say I used test.c) the program will open the test.c(no extension rather than the file "test extension (.c)")
can anyone please let me know what I am doing incorrectly?
also, when I fopen a file for reading it will read the file with the extension
Try to remove the newline from the input. By using
target[strlen(target)-1]='\0';
strlen() is used to find the length of the string which is available in string.h header file.
strlen(target)-1 which is used to find the newline location \0 the null character is pasted so the new line is removed.
If you worked with that new line the file is created as test.c?.
In disk we cannot create a file with the already exist name. So the newline is act as one character and the file name is created as like a existing name.
The problem is with your fgets(target, 99, stdin); line. After giving input 'test.c' it takes some garbage values till the end of the array. so every time it creates a new file. Instaed of fgets use scanf to get the input from user.
scanf("%s",target);
if ((fp_target = fopen(target, "a"))==NULL)
{
fprintf(stderr, "the following target file %s can not be appended", target);
exit (0);
}
If you want to use fgets to read the input from the user, after the user input make it NULL.
fgets(target, 99, stdin);
target[strlen(target)-1]='\0';

query regarding writing in a file using c

Assume that the files are .txt
Contents of first file
hello how are you
Contents of second file
I am fine
The desired result is
hello how are you I am fine
Normally what happens is that the original contents are removed and then new contents are added in it.
I want to write in the first file in such a way that its original contents are maintained and the contents of second file are concatenated in it.
Is there any way to do that?
you can append another string in file by opening it in appending mode.
FILE *fp;
fp=fopen("file.txt","a");
here the next string will append after the last pointer of file.For more info link.
Yes, you can open the file with:
fopen("fileName", "a");
This will let you append to the end if the file.
More info is here: http://www.cplusplus.com/reference/cstdio/fopen/
It would help to know how you are trying to write the file. Likely, you are looking for the append option to FOPEN:
http://www.cplusplus.com/reference/cstdio/fopen/
FILE *f = fopen("foo.txt","a");
if (f != NULL) {
/* Use f */
fclose(f);
}

Reading from a file

hello i got a problem with reading from a file, i am trying to read from a file using fscanf() and i cant seem to sort it out.
i try to read the file line by line and putting the string in a variable (buffer) each time but i cant understand how the while loop is suppose to be looking like
thanks in advance
the file that i want to read from is a txt file with this format: first line :"1234,abc,etc" second line : "2432,fjh,etc" and more lines like those i want to be able to use the fscanf method inorder to put in each loop the all line lets say "1234,abc,etc" in my string variable and so on till i dont have any more lines to read from
this is what i managed to gather so far (ofc its not the currect way to write it):
char* buffer[100];
while (fscanf(FILE *finput,"%s",buffer)!=something)
{
printf("%s",buffer);
}
i want this code to be able to print all of the lines in my code if you would be able to correct my errors i will greatly appriciate it
I feel like you should read some of these great topics first:
Trouble reading a line using fscanf()
Reading file using fscanf() in C
fscanf multiple lines [c++]
There are plenty of reasons why you should use fgets or something else instead.
Quoting from this place:
fscanf() is a field oriented function and is inappropriate for use in a robust, general-purpose text file reader. It has two major drawbacks:
You must know the exact data layout of the input file in advance and rewrite the function call for every different layout.
It's difficult to read text strings that contain spaces because fscanf() sees space characters as field delimiters.
If you know the size of file you're trying to read, you could use fread(), which is block oriented.

Resources