Parsing a Text file using C++

Thread Tools Search this Thread
Top Forums Programming Parsing a Text file using C++
# 1  
Old 09-26-2011
Parsing a Text file using C++

I was trying to parse the text file, which will looks like this

int = 4
char = 1
float = 1

like this my text file will contains lots of entries and I need to store these entries in the map eg. map.first = int and map.second = 4 same way I will keep on inserting other elements into this

Anybody can help me on this.

Thanks in advance!!!
# 2  
Old 09-26-2011
Why C++ when this can be done using awk or perl...much simpler especially if this is a one off thing.
# 3  
Old 09-26-2011
#include <stdio.h>
#include <ctype.h>

int main(void)
        char buf[512];
        FILE *fp=fopen("file.txt", "r");
        if(fp == NULL)
                 fprintf(stderr, "Couldn't open file.txt\n");

        while(fgets(buf, 512, fp))
                char *str=buf;
                char name[512], val[512];
                // Ignore whitespace at the beginning
                while(isspace(*str)) str++;
                // Ignore blank lines, comments
                if(((*str) == '\0') || ((*str) == '#') continue;

                // %[^= ] means string with no spaces or = chars.
                // then scanf expects a literal " = ", spaces optional.
                // lastly, %[^ \n] is a string with no spaces or newlines.
                // It returns '2' when it has found both strings and the = between.
                if(sscanf(str, "%[^= ] = %[^ \n]", name, val) == 2) 
                        printf("Found value line, %s = %s\n", name, val);


Last edited by Corona688; 09-26-2011 at 03:22 PM..
# 4  
Old 09-26-2011
If you really want to learn how write parser in C++ efficiently, then spend time with boost::spirit . The learning curve is steep at the beginning, but then the ROI is great Smilie

# 5  
Old 09-27-2011
Thanks for the reply.

I will try this!!!
# 6  
Old 09-28-2011
Originally Posted by NH2
If you really want to learn how write parser in C++ efficiently, then spend time with boost::spirit . The learning curve is steep at the beginning, but then the ROI is great Smilie
"Boost" is overkill for something that's accomplished with 3 libc calls, 5 if you count fopen/fclose...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing text file

Hi Friends, I am back for the second round today - :D My input text file is this way Home friends friendship meter Tools Mirrors Downloads My Data About Us Help My own results BLAT Search Results ACTIONS QUERY SCORE START END QSIZE IDENTITY CHRO STRAND ... (7 Replies)
Discussion started by: jacobs.smith
7 Replies

2. Shell Programming and Scripting

Parsing text file

I'm totally stumped with how to handle this huge text file I'm trying to deal with. I really need some help! Here is what is looks like: ab1ba67c331a3d731396322fad8dd71a3b627f89359827697645c806091c40b9 0.2 812a3c3684310045f1cb3157bf5eebc4379804e98c82b56f3944564e7bf5dab5 0.6 0.6... (3 Replies)
Discussion started by: comp8765
3 Replies

3. Programming

Parsing a text file in Tcl

Hi all, I need to parse through a text file searching for a specific string, then after I find this string read in remaining data off the line to a variable. I've tried various things and can't seem to get any to work. Any help would be much appreciated. (2 Replies)
Discussion started by: caboose57
2 Replies

4. Shell Programming and Scripting

Need help parsing a text file

I have a text file: router1#sh ip blah blah | incl --- Gi2/8 --- 11 0000 0000 355K Gi2/8 --- 11 F9FF 3840 154K Gi2/8 --- ... (1 Reply)
Discussion started by: streetfighter2
1 Replies

5. Shell Programming and Scripting

Log file text parsing

I'm new to scripting and was wondering if there was a way to accomplish what I want below using shell script(s). If there is a log file as follows, where the id is the unique id of a process, with the timestamp of when the process began and completed displayed, would it be possible to find the... (3 Replies)
Discussion started by: dizydolly
3 Replies

6. UNIX for Dummies Questions & Answers

Help parsing and replacing text with file name

Hi everyone, I'm having trouble figuring this one out. I have ~100 *.fa files with multiple lines of fasta sequences like this: file1.fa >xyzsequence atcatgcacac...... ataccgagagg..... atataccagag..... >abcsequence atgagatatat..... acacacggd..... atcgaacac.... agttccagat.... The... (2 Replies)
Discussion started by: mycoguy
2 Replies

7. Shell Programming and Scripting

Parsing text from file

Any ideas? 1)loop through text file 2)extract everything between SOL and EOL 3)output files, for example: 123.txt and 124.txt for the file below So far I have: sed -n "/SOL/,/EOL/{p;/EOL/q;}" file Here is an example of my text file. SOL-123.go something goes here something goes... (0 Replies)
Discussion started by: ndnkyd
0 Replies

8. Shell Programming and Scripting

Need help in parsing text file contents

Hi, I need some help in extracting the Exception block between the lines 21 Feb 01:18:54:146 ERROR com.orbits.frameworks.integrationframework.ValidationException - Caught exception in validateRequest() (PID=565584) and 21 Feb 01:18:55:149 INFO ... (0 Replies)
Discussion started by: Alecs
0 Replies

9. Shell Programming and Scripting

Text File Parsing

Hey Guys.I am a newbie on Bash Shell Scripting and Perl.And I have a question about file parsing. I have a log file which contains reports about a communication device.I need to take some of the reports from the log file.Its hard to explain the issue.but shortly I can say that, the reports has a... (2 Replies)
Discussion started by: Djlethal
2 Replies

10. Shell Programming and Scripting

Parsing Lines from a text file

Hello Guru's I know there are people who live to be able to help people like me. That's why I'm here! I have a text file that has a corresponding address information that I need to be able to get maybe as a Unix Function. The text file contains the following: AIX17_JB_C;... (3 Replies)
Discussion started by: sallender
3 Replies
Login or Register to Ask a Question