Need help with file input on simple C++ Project

This is a discussion on Need help with file input on simple C++ Project within the C++ Programming forums, part of the General Programming Boards category; Hello! Im new to C++, but have had quite a bit of C under my belt. I have a question ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    14

    Smile Need help with file input on simple C++ Project

    Hello!

    Im new to C++, but have had quite a bit of C under my belt. I have a question about my code. I am reading in from a file, but i have no idea what to put at a specific line.

    Could anyone help me please?

    Ill include what one line of the file looks like, as well as what variable are supposed to match up.

    Here is my code with notation at the line im having troubles...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "spkr.cpp"
    #include <fstream>
    #include <string>
    #include <math.h>
    
    
    int main(int argc, char *argv[])
    {
     FILE *f;
     FILE *fpoint;
     char c;
     int i;
     int lines = 0;
     int code;
     int eday;
     int emonth;
     int eyear;
     float time;
     char sletter;
     float ecost;
     float rcost;
     char state1;
     char state2;
     char expletter;
     int bday;
     int bmonth;
     int byear;
     int seats;
     char *first;
     char *last;
     char *name;
     int x;
    
          //####LINE COUNTING BLOCK##############################
         //****************************************************** 
    
                      f = fopen(argv[1], "r");
    
                         if(f == NULL)
                              cout << "File not found";
    
                               while((c = fgetc(f)) != EOF)
                               if(c == '\n')
                               lines++;
    
                               fclose(f);
    
                               if(c != '\n')
                               lines++;
      
      
     
                               i = lines;
         //initializing the size of the class array according to how many lines are in the file
         Spkr UTAspkr[i];
         //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         //****************************************************** End of counting block
    
    
          ifstream myReadFile;
                   myReadFile.open(argv[1]);
                   
                   if (myReadFile.is_open()) 
                      {
                      while (!myReadFile.eof()) 
                            {
    
                             //This is where i need help !!!!!
                         myReadFile >> ????????;
    
                            }
                      }
                            
    }

    The .TXT files first line looks like:

    100 24 9 2010 19.00 M 0.00 0.00 NY L 29 7 1953 Ken Burns 525 An Evening with Ken Burns

    and the variables i need to match up are:

    int code = 100
    int eday = 24
    int emonth = 9
    int eyear = 2010
    float time = 19.00
    char sletter = M
    float ecost = 0.00
    float rcost = 0.00
    char state1 = N
    char state2 = Y
    char expletter = L
    int bday = 29
    int bmonth = 7
    int byear = 1953
    int seats = 525
    char *first = Ken
    char *last = Burns
    char *name = An Evening with Ken Burns

    Like i said, im pretty new to C++, i looked around but it was hard to find something that fit my problem.

    The problem im having is what to put after myReadFile >>

    Can anyone lend a hand ?

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Why are you mixing file handling from c with c++ stream objects? just use all c++ here.

    You should use the 'getline' function and then tokenize your string, the use of a stringstream object will let you obtain an integer value from the string where required and it will also allow you to do a simple tokenization (is that a word??..!)

    really you need to look through the 'How do i...' sections on the FAQ Pages for lots of information on this and other useful things.

    oh and look up some information on properly indenting your code, its not bad, but there are a few wild things going on
    Last edited by rogster001; 12-08-2010 at 05:30 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    Thank you so much, like i said I'm very new to C++ and that feature makes me look forward to learning this language ... C++ seems to be a lot more sleek

    I'll do better with my indentation, i just kind of rushed something together for the forum post because i didn't have access to my SSH server files.


    Thank you so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

Tags for this Thread


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