LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2011, 08:39 AM   #16
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32

Ah yes I realized that with the missing #include <vector> afterwards. And have corrected the code to use push_back() for the vector. Thanks!

This is the new version. The only thing that isn't working is the following.
Code:
test.cpp:22: error: aggregate ‘std::stringstream strstream’ has incomplete type and cannot be defined
Also tried to use static_cast<int>(value) but didn't work. Any idea why? It'd just say the following.
Code:
test.cpp:46: error: invalid static_cast from type ‘std::string’ to type ‘int’
Code:
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>

std::string name, age, sex;
std::string field, value;
std::string ifilename = "config.txt";
std::string lfilename = "out.log";
std::ifstream ifile(ifilename.c_str());
std::ofstream ofile(lfilename.c_str());
bool bname = false, bage = false, bsex = false;
int count = 0, valueInt;
std::stringstream strstream;

struct TypeStructConfig {
  std::string name;
  int age;
  char sex;
} ;

typedef std::vector<TypeStructConfig> TypeVectorStructConfig;
TypeVectorStructConfig VectorStructConfig;
TypeStructConfig StructConfig;
/*
 * 
 */
int main(int argc, char** argv) { 

    while (getline(ifile, field, '=')) {        
        getline(ifile, value);
        
        if (field == "NAME") {
            StructConfig.name = value;
            bname = true; 
            count++;
        } else if (field == "AGE") {
            //StructConfig.age = static_cast<int>(value);
            strstream << value;
            strstream >> valueInt;
            StructConfig.age = valueInt;
            bage = true;
            count++;
        } else if (field == "SEX") {
            StructConfig.sex = value[0];
            bsex = true;
            count++;
        } else {
            ofile << field << " is an unknown configuration parameter.\n";
            return 1;
        }
        
        if (count == 3 && (bname == false || bage == false || bsex == false)) {
            ofile << "Not all required parameters are set.\n";
            return 1;  
        } else {
            VectorStructConfig.push_back(StructConfig);
            count = 0;
        }
    }
    
    return 0;
}

Last edited by grob115; 12-06-2011 at 08:40 AM.
 
Old 12-06-2011, 08:54 AM   #17
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32
Okay problem solved for the following error. I just need to add #include <sstream>.
Code:
test.cpp:22: error: aggregate ‘std::stringstream strstream’ has incomplete type and cannot be defined
However, if anyone can advise why the static_cast failed, that'd be much appreciated!
 
Old 12-06-2011, 09:34 AM   #18
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by grob115 View Post
Hi, reason there wasn't a "main" function was because I was thinking of putting this into one of the cpp files for the project, and use include this from the main source file.
That still wouldn't make it a valid *.cpp, because in C++ you can't just dump code into middle of nowhere. You need functions, methods or classes.

Quote:
Originally Posted by grob115 View Post
However, if anyone can advise why the static_cast failed, that'd be much appreciated!
Because std::string is not int, and does not even remotely resemble it.
EIther use istringstream or atoi to convert string to int.

I suggest to get C++ book, read it and walk through excercises. "Frontal assult" (i.e. "let's keep modifying program till it magically starts working") doesn't work well when learning C++ - you need a book.

Last edited by SigTerm; 12-06-2011 at 09:36 AM.
 
1 members found this post helpful.
Old 12-06-2011, 11:33 AM   #19
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32
Thanks. Have been reading the cplusplus.com tutorial. Think there are a few other ones also. This is also a good one.
http://www.learncpp.com/cpp-tutorial/19-header-files/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I need to parse a text file in shell scripting shanti sree Linux - Newbie 4 10-12-2011 02:09 PM
[SOLVED] Parse Text File With Perl unsymbol Programming 1 03-05-2011 05:36 AM
[SOLVED] sed - parse text file elexx Programming 8 02-22-2011 10:19 AM
Parse multiple variable from text file with bash Goni Programming 2 07-13-2010 02:25 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration