Home Forums C Programming Read in from a file with specific syntax …

Viewing 1 reply thread
  • Author
    Posts
    • #2044
      toton
      Participant

      Hello, thank you for taking the time to review my problem. I’m trying to read in from a file which has specific syntax (shown below) and recognize part1 and part2. It should compare part1 with an internal value to be returned from later code. In short:
      The file will consist of the following:
       
      part1:part2
      etc…
       
      I need to read each line and recognize part1 as a variable and part2 as a variable (seperated by the colon, which is to be ignored). If I can get that working, I have the rest sorted out (i think). All feedback is greatly appreciated!!

    • #3292
      Humayan
      Participant

      Heres an old program that I have that reads an int then a float on each line:
       

      /****************************************************************
      * File Name : cgt.cpp
      * Date : April,18,2006
      * Comments : new project
      * Program Shell Generated At: 10:33:48
      =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
      #include
      #include

      int main()
      {
       ifstream inputFile ( "c:\programs\CSC2133N.TXT" );
       if ( inputFile.fail() ) //check if file opened
       {
        cout << "File Did Not Open!" << endl;
        return 1;
       }
       
       int i;
       double d; //use better names here!!!!!
       
       inputFile >> i;
       while ( ! inputFile.eof() )
       {
        inputFile >> d;
        cout << i << " " << d << endl;
        inputFile >> i;
       }
       inputFile.close();
       return 0;
      }
Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.