-
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;
}
}
-
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
-
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