calling a spesific line from input file

This is a discussion on calling a spesific line from input file within the C++ Programming forums, part of the General Programming Boards category; hi all!! I am a beginner in C++ and need your help. I am trying to call a specific line ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    Post calling a spesific line from input file

    hi all!!

    I am a beginner in C++ and need your help. I am trying to call a specific line from an input file and write it to the screen. can you tell me what the algorithm of it?. f. ex :

    a 1 x
    b 2 y
    c 3 x
    d 4 y

    how can I get the '3 x' as output if I know the value 'c'.

    thanks in advance.

  2. #2
    Unregistered
    Guest
    I would try reading the first character (there are a number of functions for this). If it is the 'c' the use getline to read the whole line after the 'c'

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    3
    thanks for reply.
    this is the problem...I do not know how to output the 3 x using either getline or any other algorithm.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If the file is exactly as you have indicated then you could do this:

    Code:
    ifstream fin("file.txt");
    char ch;
    int  num, dummyInt;
    int var, dummyVar;
    char found = 'n'
    
    //find first occurence of ch with value of 'c'
    while(fin && found == 'n')
    {
      fin >> ch;
      if(ch == 'c')
      {
         fin >> num;
         fin >> var;
         found = 'y';
       }
        else 
        {
           fin >> dummyInt;
           fin >> dummyVar;
         }
    }
    //proceed with rest of program.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    Smile thanks

    this is what I needed.
    I have just applied your solution and it worked!!
    thanks a lot.!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  3. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 01:48 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21