files question

This is a discussion on files question within the C++ Programming forums, part of the General Programming Boards category; Hi, I nee to write program that will arange my text files. Every row in file is supposed to begin ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    files question

    Hi, I nee to write program that will arange my text files.
    Every row in file is supposed to begin with '{'. If it is not the case then
    I need the row that begin with every other character to place in current line
    not write as new row. So in my result file every line should start with '{'.
    I think I manage to do this and test it on one file and it seems to work fine.
    I have three question:
    1. Is there a better nad more elgant way to handle this
    2. Do I have to close files.
    3. (Not connected with upper two) When I want to write something in binary file
    I open with something like this: ofstream ofs("name",ios_bas:binary);
    and want to write I use something like this:
    ofs.write((char*)&matrix[i][j],sizeof(double));
    My question is way is ti necessary to erite (char *) and what it means?
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	char current[BUFSIZ];
    	char next[BUFSIZ];
    	int i=0;
    	ifstream in("Bow.sub");
    	ofstream out("BowlingPr.sub");
    	while(in.getline(current,BUFSIZ,'\n'))
    	{
    		i++;
    		in.getline(next,BUFSIZ,'\n');
    		if(current[0]=='{')
    		{	
    			if(i!=1)
    				out<<'\n';
    			out<<current;
    		}
    		else
    		{
    			out<<current;
    		}
    		if(next[0]=='{')
    		{
    			out<<'\n';
    		}
    		out<<next;
    		
    	}
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    23
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi (refer level 2 I think that should give you good idea )

    Regarding your last question (char *) means "type casting " The concept is similar in C/C++

    http://www.cplusplus.com/doc/tutorial/tut5-4.html
    This link should be very helpful for that other try googling for type casting in C++

    Hope it helps
    Nipun

    Nipun

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Thanks for reply. I know what is casting but don't know why I need to cast to char * something that is other type. In this case matrix of double numbers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question on reading files in C
    By TaiL in forum C Programming
    Replies: 12
    Last Post: 10-05-2008, 09:48 PM
  2. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  3. input/output files question
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2005, 12:38 PM
  4. Question about ".o" files
    By Jez_Master in forum C++ Programming
    Replies: 1
    Last Post: 04-11-2002, 01:54 AM
  5. Using files: Wacked Question
    By Denethor2000 in forum C++ Programming
    Replies: 2
    Last Post: 04-05-2002, 11:54 PM

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