question about reading files in

This is a discussion on question about reading files in within the C++ Programming forums, part of the General Programming Boards category; Hi, I am having a lot of trouble with reading a file in, but being able to ignore the first ...

  1. #1
    smd
    smd is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    question about reading files in

    Hi,

    I am having a lot of trouble with reading a file in, but being able to ignore the first few lines. This is the file I'm reading in:

    ######################
    #something
    #####################


    somethingtobereadin1

    somethingtobereadin2


    I want to be able to ignore the first 3 lines and whitespace, and read in the strings "somethingtobereadin1" and "somethingtobereadin2". I am posting the code I have written so far, I tried reading it in and then using the ignore function. However, I am having lots of trouble trying to figure this out and will appreciate any help anyone can provide. THanks for any help.

    Here is the code:
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    int main()
    {
      ifstream infile;
      infile.open("readinfile");
    
      char buffer[50];
      string temp; 
     
    while (!infile.eof()){
    
      infile>>buffer;
      infile.ignore(100,'\n');
     
      infile>>temp;
     
       cout<<temp<<endl;
    }
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    int main()
    {
      ifstream infile;
      infile.open("readinfile");
    
      string temp;
     
      for(int i=0; i<5; i++)
      {
            getline(infile, temp); 
      }
      
      cout<<temp<<endl;
    
      getline(infile, temp);
      getline(infile, temp);
      cout<<temp<<endl;
    }
    
    }
    Last edited by 7stud; 08-23-2003 at 07:00 PM.

  3. #3
    smd
    smd is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    still having trouble

    Hi, thanks for the response.

    However, the code posted did not work and I am still having trouble figuring this out. I want to be able to ignore the first 3 line of comments.

    And then want to be able to read in the rest of the strings, one line at a time till the end of file.

    What I was trying to do in the code posted in the previous posted code is to readin the buffer of a certain size and then ignore it and then start reading the strings till the end of file.

    I am not sure if this is possible, but would appreciate any additional help on this. Here is the read in file and the reading in code below it.

    ######################
    #something
    ######################

    somethingtobereadin1

    somethingtobereadin2

    The code:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    int main()
    {
      ifstream infile;
      infile.open("readinfile");
    
      char buffer[50];
      string temp; 
     
    while (!infile.eof()){
    
      infile>>buffer;
      infile.ignore(100,'\n');
     
      infile>>temp;
     
       cout<<temp<<endl;
    }
    
    }

    Thanks so much for any help anyone can give !

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Ignoring or reading--it's the same difference.
    Code:
    #include<iostream>
    #include <fstream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	ifstream infile("readinfile");
    
    	string temp;
    
    	for(int i=0; i<3; i++)
    	{
    		getline(infile, temp); 
    	}
    	
    	int count = 0;
    	
    	while(!infile.eof())
    	{ 
    		getline(infile, temp);
    		count++;
    		
    		if(count%2 == 0) //if count is even
    		{
    			cout<<temp<<endl;
    		}
    	}
    
    	return 0;
    }

  5. #5
    smd
    smd is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    Unhappy still having trouble -

    Hi,

    Thanks for the response.

    I am still having trouble with a piece of code: I have been trying to read in a file, ignore the first 3 lines and then read in the 2 strings that follow. I can open the FILE but can't figure out how to be able to ignore the first 3 lines and then read in the 2 strings that follow.

    Here is the readinfile:

    ###################### //ignore this
    #something //ignore this
    ###################### //ignore this

    somethingtobereadin1 //read this string in

    somethingtobereadin2 //read this string in

    Here is the code:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    int main()
    {
      ifstream infile;
      infile.open("readinfile");
    
      char buffer[50];
      string temp; 
     
    while (!infile.eof()){
    
      infile>>buffer;
      infile.ignore(100,'\n');
     
      infile>>temp;
     
       cout<<temp<<endl;
    }
    
    }
    I tried the code someone suggested, however I get no output when I ran it.

    I have been stuck on this and would appreciate any help or guidance on how to do this.

    THanks in advance for any help anyone can provide.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Put:
    Code:
    using namespace std;
    after your includes.

    You should make sure the file you are reading doesn't have an extension. However, I would assume you are reading a txt file, and in that case append ".txt":
    Code:
    infile.open("readinfile.txt");
    To make sure the file is being opened:
    Code:
    infile.open("readinfile.txt");
    if (infile.fail())
    {
      cerr<<"Unable to read from file.";
      cin.get();
      exit(EXIT_FAILURE);
    }
    Maybe that will help some...I might post back later

    edit:
    You could check to see if the first character in the line is a "#" then ignore the rest by using infile.ignore(100,'\n');
    Try using infile.get(char) and then check whether that char is the "#" character
    Last edited by JaWiB; 08-24-2003 at 08:30 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I tried the code someone suggested, however I get no output when I ran it.

    Then:
    1) You don't know how to run programs
    2) You don't know how to open a file for reading
    or
    3) Your input file is not the same as you claim

    JaWiB,
    You could check to see if the first character in the line is a "#" then ignore the rest by using infile.ignore(100,'\n');


    That would also ignore any lines after the first three lines that had a '#' at the beginning of the line, and "something to be read in" can begin with a '#' character couldn't it? Your test would also designate blank lines as valid input.
    Last edited by 7stud; 08-24-2003 at 08:47 PM.

  8. #8
    smd
    smd is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    43
    Hi,

    thanks for the quick response,

    I tried the using namespace std;

    and I also changed the code based on what 7stud posted,

    unfortunately when I run that code, I get no output at all.
    When you execute it, does it give the output strings

    "somethingtobereadin1" and "somethingtobereadin2" ?

    THis file is set in terms of 3 lines with comments that need to be ignored and the strings will have no '#' in front. I checked the file name as well (the one being read in).

    Is there maybe another reason I'm getting no output at all??

    Thanks a lot for all the help.
    Last edited by smd; 08-24-2003 at 08:47 PM.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I tried the using namespace std;

    You said your code gave you no ouput, but your code won't even compile without the using namespace line. Did your code not compile or did you get no ouput? Or, do you consider those the same?

  10. #10
    smd
    smd is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    43
    The code did compile without the using namespace std, and when I ran the a.out, there was no output.

    And then I ran the code posted (which had the using namespace), and that compiled as well, and I ran the a.out, and there was no output.

    I am doing this on solaris, and I double checked the name of the file being read in, unfortuantly , still getting no output.
    when you ran this code, did it output the 2 strings successfully??

    THanks for all your time and help!

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Of course! Otherwise, I wouldn't have posted it. But it's dependent on the exact format of the input file you posted. If your format actually has an extra blank line at the beginning of the data to be read in, then instead of discarding the blank lines, the program will discard the data to be read in and display the blank lines. However, you should have enough information to alter it to fit your input file.

  12. #12
    smd
    smd is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    thanks

    Thanks for your help!!! There was something wrong with my compiler....but it works now...thanks again for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about IOStream and reading strings from files
    By kingpinzs in forum C++ Programming
    Replies: 22
    Last Post: 12-13-2005, 10:29 AM
  2. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  3. Question About Reading Files
    By Zildjian in forum C Programming
    Replies: 1
    Last Post: 09-28-2003, 05:31 PM
  4. Reading files in a directory
    By roktsyntst in forum Windows Programming
    Replies: 5
    Last Post: 02-07-2003, 09:04 AM
  5. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM

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