Parsing multiline string in C - c

I'm trying to parse a file using C language with a format like this:
FILE, "CONTENT", PATH
So, for example I have an input like this:
file.txt, "This is a simple file", /home/user
config.txt, "#Used to do any configurations
#ID = 'PC1'
VERSION = 1.25", /etc
First, I read a line of the file an put it into buffer, next I do:
sscanf(buffer, "%[^,], \"%[^\"]\", %s", name, content, path)
But, however I only get correctly single line inputs. Is there a way to force sscanf to accept new lines on CONTENT string?

For non-trivial parsing, I would tecommend using Flex and Bison

Related

Is there a way to import a txt or rft file from a dxl script?

I am trying to import a text file to my dxl script to use the words included in the txt as strings for a comparation. Is there a way to do so?
Thank You in advance.
Well, yes, of course. You can read the context of a file using a Stream variable, like in
Syntax:
Stream read [binary] (string filename)
bool end (Stream strm)
// Read from a stream. var can be of type: Buffer, char, real, int, string
<Stream> >> var [ >> var [ >> ...]]
// Special Read Functions:
<Stream> >= <Buffer var // Read one text line including whitespace
<Stream> -> <Buffer var> // Read one text line. Omit leading whitespace
string readFile (string filename) // Returns the contents of a file a string
Example:
string filename = "c:/datafile.txt"
Stream input = read filename
real cost
for (input >> cost; !end input; input >> cost) {
print cost "\n"
}
close input
In your case, you would read the stream into a variable of type Buffer. Depending on your needs, you would also have to split a line into several words, e.g. seperated by space. If you do not need to split the values, you could also read the stream directly into a string variable.
Reading an RTF file is much more complicated as far as I know. I think you will have to know about the internal structure of RTF files and be able to distinguish formatting information from real string or use a COM library. Perhaps it is possible to use one of the richText features of DOORS, look at the help or at Google, I am not so fluent in DOORS w/ RTF.

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

Search txt file then print at console txt file and colour the string which search

I made a program which searches for a string, here is code: When we search word in txt file how we could colour in txt file
I want to print at console all text with underline string. How can I do this?
I am using linux
It depends on what your terminal is capable of. Check your terminfo, and see (for instance) http://man7.org/linux/man-pages/man4/console_codes.4.html for some coding variants (that page calls it 'underscored').
For me, on Mac OSX's Terminal, printing the following escape code works:
"\x1b[4m"
-- defined as PP_UNDERLINE, I can use it like this:
printf ("processing file " PP_UNDERLINE "%s" PP_RESET, argv[i]);
to get underlined text in my terminal output:
Something like this will generally work:
printf("Normal \x1b[4mUnderlined\x1b[24m Normal.\n");

Is there a way for reading key value on many lines from a file?

I have the following config file:
[GENERAL_CONFIG]
filter_subnetworks = 192.168.105.0/24 1.1.0.0/16 192.168.105.0/24
192.168.105.0/24 1.1.0.0/16 192.168.105.0/24
192.168.105.0/24 1.1.0.0/16 192.168.105.0/24
and i want to read all subnetworks with g_key_file_get_string_list (gkf, "GENERAL_CONFIG", "filter_subnetworks", &s_len, &error) but this function read one single line.
It looks like your input file doesn't comply with the formatting required by the glib Key-value file parser functions.
All key values should be on a single line, and you should have an explicit list separator character (not just space) such as ; or ,, see the g_key_file_set_list_separator() function.
Convert the file to comply with the required glib format if you're going to use their API. Note that as soon as you save your file back out, it will use the glib API, so there's little point in "tricking" it to load something else.

Replacing a word in a file, using C

How do I replace a word in a file with another word using C?
For example, I have a file which contains:
my friend name is sajid
I want to replace the word friend with grandfather, such that the file is changed to:
my grandfather name is sajid
(I am developing on Ubuntu Linux.)
Update:
I am doing filing in C. I have created a .txt file and write some data into it, but as my program progresses I have to search some text and replace it with the other words. The problem I am facing is that suppose in my file I wrote
"I bought apple from the market"
If i replace apple with pineapples as apple has 5 char and pineapple has 9 char it will write it as
"I bought pineapple m the market"
It also has affected the words written after apple.
I have to do it using C, not a command line script.
How about using the exiting Linux sed program?
sed -i 's/friend/grandfather/' filename
That will replace friend with grandfather in the existing file. Make a copy first if you want to keep the original!
Edit:
Alternatively, load the file into an STL string, replace 'friend' with 'grandfather' using a technique such as this, and save the new string into a file.
As you've realized, you won't be able to make the change in place in the original file.
The easy solution is to read each string from the original file and write it to standard output, making the replacement as necessary. In pseudocode:
open original file
while not at end of original file
get next string from original file
if string == "friend"
write "grandfather" to standard output
else
write string to standard output
end while
You can then redirect the output to another file when you execute it, similar to how sed and awk work.
Alternately, you can create a destination file in the code and write to it directly. If you need to replace the original file with the new file, you can use the remove() and rename() library functions in stdio.

Resources