help needed with file I/O

This is a discussion on help needed with file I/O within the C++ Programming forums, part of the General Programming Boards category; I have written a code for getting inputs from a file. I need to parse the input string and then ...

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

    help needed with file I/O

    I have written a code for getting inputs from a file. I need to parse the input string and then putput it to another file. The input file contains multiple lines. I am not sure what I am doing wrong. Please see the attached code.

    Code:
    ****************************************************
    
    
    
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
    string line;
    string sn;
    string cn;
    int pos7 = 0;
    
    
    ifstream myfile;
    myfile.open("passwd");
    if (myfile.is_open())
    {
    		while (! myfile.eof())
    		//while (! getline (myfile,line));
    			{
    			getline (myfile,line);
    			int pos1=line.find(":",0);
    			string uid=line.substr(0,pos1);
    			int pos2=line.find(":",pos1+1);
    			int pos3=line.find(":",pos2+1);
    			int pos4=line.find(":",pos3+1);
    			int pos5=line.find(":",pos4+1);
    			if (pos5 == pos4+1) 
    			{
    						cn=uid;
    				}
    				else{
    			cn=line.substr(pos4,pos5-pos4);
    				}
    				int pos6=cn.find("",0);
    				if (pos6!=0)
    					{
    							pos7=cn.find("",pos6+1);
    							if (pos7!=0)
    							{
    								sn=cn.substr(pos7,pos7-pos5);
    							}
    							else 
    							{
    								sn =cn.substr(pos6,pos6-pos5);
    							}
    						}
    				else {
    					 sn=cn;
    				}
    ########################################
    The output gives a abnormal program termination. when i try and print out all the values everything is fine except the pos7 value which is shown as negative. also the input line is only taken as the last line of the file. Please any kind of guidance will be welcome.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> pos7=cn.find("",pos6+1);
    What are you trying to find? An empty string?

    >> //while (! getline (myfile,line));
    Why is this commented out? That is the preferred way to read through the file, not using eof(). Putting getline in the while control will catch any error and only run the loop if theread succeeds.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    14
    I was trying different methods and hence it is commented.

    I am trying to find a white space.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    If you just want to find a space, use find with " " (note the space inside the quotes). If you want to find any space, tab or newline, use find_first_of with " \t\n", which will find any of those three characters. Since you are using getline, there won't be any '\n' characters, but you can find spaces or tabs with that method anyway.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    14
    Thanks a lot for that. I am not yet sure why my whole file is not being read it in line by line. Like in my output all I am getting is the output for the first line.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Your output code isn't shown above so it's hard to say. However, if getline fails, it will leave the old value in line. This might be why the same value is displayed over and over again in the output. It is also why I would suggest putting getline in the while loop. You can check the fail state of the file stream to make sure. If fail() returns true then getline won't actually read in the line.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    14
    I am trying the code with getline in the while loop. when I print the line in the putput, all I am getting is the first line. I am not sure how do i make the getline move to the next line.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    It should move to the next line automatically. Show the rest of the code that got cut off above.

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    14
    Code:
    		ofstream file1 ("passwd_sorted.txt");
    		if (file1.is_open())
    		{
    			file1 << "dn:cn=";
    			file1 << cn;
    			file1 << "cn:";
    			file1 << cn;
    			file1 << "\nsn:";
    			file1 << sn;
    			file1 << "\nuid:";
    			file1 << uid;
    			file1.close();
    		}
    
    		}
    			}
    
    		else cout << "unable to open file";
    		return 0;
    
    
    				
    myfile.close();
    
    
    
    }
    This is the rest of the code where I am doing nothing but putting the parsed data into another file

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    You need to open your ofstream in append mode so that each line will be added to the end instead of overwriting the whole file.

    Also, I noticed that you are comparing your pos variables against 0. You should be checking them against string::npos (which is just -1). Otherwise, if a find fails, you will be using -1 as a string index and potentially having your program crash.

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    14
    The getline function is not moving to the next line automatically. logically if am not opening the file in append mode then the last line should have been printed but only the first line is getting printed... Please help..or is there any other way of doing this?

  12. #12
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Are you using VC++ 5 or 6?
    http://www.dinkumware.com/vc_fixes.html
    Look at the one for the <string> library, see if fixing that helps.

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    14
    I am using VC++6. Changed the string file as per the suggestion but the input stream just does not move after the first line.
    Should i get the input character by character...will that help??

  14. #14
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Can you repost your current/most upto date code (all of it, the input and output parts).

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    14
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
    
    string line;
    string sn;
    string cn;
    int pos7 = 0;
    
    
    ifstream myfile;
    myfile.open("passwd");
    if (myfile.is_open())
    {
    		while (!getline (myfile,line))
    			{
    			int pos1=line.find(":",0);
    			string uid=line.substr(0,pos1);
    			int pos2=line.find(":",pos1+1);
    			int pos3=line.find(":",pos2+1);
    			int pos4=line.find(":",pos3+1);
    			int pos5=line.find(":",pos4+1);
    			if (pos5 == pos4+1) 
    			{
    						cn=uid;
    				}
    				else
    				{
    			cn=line.substr((pos4),pos5-pos4);
    				}
    				int pos6=cn.find(" ",0);
    				if (pos6!=-1)
    					{
    							pos7=cn.find(" ",pos6+1);
    							if (pos7!=-1)
    							{
    								sn=cn.substr(pos7+1,(cn.length())-(pos7+1));
    							}
    							else 
    							{
    								sn=cn.substr(pos6+1,(cn.length())-((pos6+1)));
    							}
    						}
    				else 
    				{
    					 sn=cn;
    				}
    
    		ofstream file1 ("passwd_sorted.ldif");
    		if (file1.is_open())
    		{
    			file1 << "dn:cn=";
    			file1 << cn;
    			file1 << "cn:";
    			file1 << cn  << endl;
    			file1 << "sn:";
    			file1 << sn << endl;
    			file1 << "uid:";
    			file1 << uid << endl;
    			
    					
    		}
    
    		}
    			}
    
    		else cout << "unable to open file";
    		return 0;
    
    myfile.close();
    file1.close();
    
    }
    This is the code am working on...I am not yet sure why the next line is not being read or i am doing some other mistake in the output to the file???

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 03:47 AM
  2. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

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