Issues with reading and writing in a file

Hello Experts,

I am trying to make a program to store records of a company in a txt/doc file. The record includes both integers and characters. However, while I am using write function, my integers are also converted to char because of write's syntax.

1
2
3
4
5
6
7
ofstream out;
	out.open("E:/DATA.doc",ios::out|ios::app);
	
	for(q=p;q!=NULL;q=q->right)
	{
		out.write((char*)&q,sizeof(q));
        }


As you can see it is mandatory to typecast it to char, so this issue is coming. I want to know if there is any other function which can pass integer, float as well as char values to a file.?

Thanks in advance,
Abhishek
Last edited on
Um... why not use << like one does with std::cout?

-Albatross
@Albatross

Thanks for reply.
I want it in file, not on console.
In C++, file and console i/o are handled in the same way, as streams. If you think about it, your object shouldn't care where it's been written to or read from, only that it can be written or read.

You should define << and >> operators on your records and write them to the stream, it doesn't matter what kind of streams they are.

If you post your record, I'm sure someone will help.
@kbw

Thanks for quick response.

My records are like : (only part of it though)
1
2
3
4
5

Employee ID        Name           Age         Courses Done           Certifications         
10XA52                Raj               23           xxxxxx                       xxxxx

 


Its an excel sheet which have lots of other columns as well. So in short its a mix of integers, floats, and char. In some cases it can be integer and char in one column as in Employee ID.

Regards,
Abhishek
What format is your Excel file? If it's a text format like .CSV, it's reasonably easy. If you use Excel native formats, you'll have to use Excel as a COM object to read it.

Are you trying read and write these files or just write them?
I am not sure about the same. I am simply creating an excel file as:

1
2
ofstream out;
	out.open("E:/DATA.xls",ios::out|ios::app);


I want to both read and write from the file.
Nobody has yet answered your question. The answer is no. Its impossible to tell the file that you want to pass an integer or float. Thats because files are made up of a bunch of characters (stream) and only bunches of characters (streams). This means when you read a character array (stream): "24" from a file, you must convert it to an integer which is easy using the "atoi" function. The character 0 has a numerical value 48. Which means everytime you read the number 7 from a file its not stored as an integer. What youre looking at is the character seven, which has the numerical value of 47 7=54. And thats actually what the atoi function does. It does this conversion. Heck, heres the function right here!:

1
2
3
4
5
6
7
8
9
/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9';   i)
n = 10 * n   (s[i] - '0');
return n;
}


Thus if you want a text file to store integers, make it a char save it to that file. Then you have to read it as char (because once again files are one big char array). If you read it as character 10, pass this value to the atoi function (character to integer) or even character to float or whatever (other conversions exist). Then only can you apply arithmetic to these numbers. However I ask myself. why do you need to pass integers to your file? Because of age? Why does age have to be an integer? Remember, you store "age" just because someone must see it on the screen. And if someone is 24 years old, why not just save it as two character array. A 2 and a 4.

On a sidenote I use the C method:

I dont know just how unorthodox this method is but it works for me :D. Highlight the entire excel spreadsheet and paste it in a txt document and save it in a directory. Youll notice its columns are actually seperated by a "tab" and its rows seperated by new line characters n. Take note of that because once you do its easy to manipulate.

To "use" this text, load it into an appropriately sized character array.

Open the file for reading:
1
2
3
4
5
6
7
8
   FILE * pFile;
   int i;
   char text_file[36000];
   pFile = fopen ("the_name_of_the_text_file_you_want.txt","r");
int getc(FILE *pFile);

for(i=0;text[i]!=EOF;i  )
 text[i]=getc(pFile);


Now your text file is loaded into a character array.
Last edited on
@Sputnik.

Thanks for your response. It was indeed a good explanation. I completely agree with you with taking age as char. I have used the same as same to save memory also. However, this was just a part of a big excel sheet. There are lot more integers. Anyways, I agree with atoi concept of yours.

Apart from this I have one very different problem. In this excel sheet, I am maintaining a database. So, the program is run many times and excel sheet is updated. However, if you can consider the headings in excel sheets namely Employee ID, name , age etc, they must be written only once, i.e. only when the sheet is blank (or at very first entry). So is there a way to detect if excel sheet is empty or not and i can give headings from the programs.?

Thanks and Regards,
AbHI
You can't write an Excel spreadsheet like that. We'll you can, but it's exceedingly complicated.

The recomended way to process an XLS or XLSX file is to use Excel to do it thru its COM interface. I don't have those details to hand, but it's been discussed on the forum and I have done if before. I have written XLS files directly before too, it was difficult enough when the method was documented never mind now.

If you do use the Excel in your program, you can query the spreadsheet too.
ok I will try to find out how to interface Excel's COM interface and let you know.

Thanks for help.
Abhishek
Not too sure what you mean with regards to the second question... But if its what I think you mean then cant you save the headings employee name age ID number etc etc inherently into the source code of the programme? So your headings will load up everytime from the character arrays "name" "number" that are declared from within the programme.
@Sputnik

Yes I do not want that the headings should load every time the program is executes. That is why I want to detect if the excel sheet is empty or not. If its empty then only the headings should load.
Topic archived. No new replies allowed.