Thread: finding a particular string in a readin file

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    finding a particular string in a readin file

    Hello,

    I have a file that I'm reading in.. i.e. :

    \version: someexample
    \file: somefile
    \testsize: sometest
    \datapoint: somepoint

    when I read it in I need to be able to look at th file and check for a particular thisg: example: I need to be able to check for the string "\testsize: " and then bring in the (integer) sometest and store it in some variable.


    I know how to read in a file:

    int sometest;

    ifstream infile("myfile");

    //I am not sure how to check for the string "\testsize: " and
    // read in the number that it is assigned...

    infile >> sometest;

    any help will be greatly appreciated.... thanks

  2. #2
    Unregistered
    Guest
    use the getline() command and then compare the string that you retrieved against what your looking for with strncmp()

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    need help with finding a string and then reading its value

    hello,

    I am hoping someone can help me...I'm having quite a bit of problems with this...

    I outlined the problem above...but basically I am reading in a file, I'll give a small sample of what the file contains:

    /scan size: 123
    /scan size2: 1234
    /zsize: 90

    this list is long and most of the components are of not interest.
    so, while reading in...I want to be able to look for string: "/scan size: " or "/zsize: ", and I want to read in the numbers that are listed for those paramenter, i.e: for "/scan size: " I want to store 123, for "/zsize: " I want to store 90, I can't figure out how to do this....this is what I wrote:



    void readheader(char *infile){

    ifstream input(infile);//this is just the file that I'm getting
    char filein[30];
    strcpy(filein, infile);

    char str[] [20]= { "\Scan Size: ", "\Z sensitivity: ", "\Z max: "};
    int n;
    float scansize1;


    ifstream in(filein);

    if (in.is_open()) {

    while(in ){
    if(strncmp (str[n], "\Scan Size: ", 12) == 0) {

    in>>scansize1; //so here it would see "\Scan Size: " and
    //should read in the value that's listed beside it...

    } } }

    //just to check if it did read the right number

    cout<<"this is the scansize: "<<scansize1;

    in.close();

    }//end readheader

    I ran this, it complied, but not doing anything...I'm really not sure how to code this and need help.....thanks for any help....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. finding out a string from a text file
    By Brij in forum C++ Programming
    Replies: 1
    Last Post: 01-21-2007, 03:22 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM