c++ How to read from a file placed on my desktop

Hi guys i want some help...

I've a file named "assad.txt" on my desktop. I want to read it through C++ files. Can anybody tell me the syntax how can i open this file in Visual C++ 6.0??

what i am doing is...

fstream file; // object i made

file.open("assad.txt", ios::in); // Open in read mode

file.read((char *)&rec, sizeof(rec)); // file reading syntax


what makes me confuse is what should i write instead of &rec and sizeof(rec)??


pls help me out..... !!!!!!!

thnx


Last edited on
Perhaps you can start by reading this site's tutorial on files:
http://cplusplus.com/doc/tutorial/files/
It will show you the easiest way to read lines of text from a simple text file.

In your code above, the line you ask about reads sizeof(rec) characters into the buffer rec. It's a bit difficult to give details without seeing the definition of rec, though.
let me upload my code, u'll easily understand and guide me!!
#include <iostream.h>
#include <windows.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>


void main()
{
fstream file;


file.open("F:\assad.txt",ios::in);

file.read((char *)&file, sizeof(file));

}

i replaced (rec) with 'file"... duno its right or wrong... guide me pls wt to do???
Firstly, you should check if the file was opened successfully, using file.is_open(), which returns true if it is opened and false if not (e.g. if the file wasn't found).

Anyway, to the point. From this site's documentation:
[quote=cplusplus.com]
istream& read ( char* s, streamsize n );

s
Pointer to an allocated block of memory where the content read will be stored.

n
Integer value of type streamsize representing the size in characters of the block of data to be read.
[/quote]

So it reads n characters from the file into the buffer s. You must therefore create a buffer to read into, for example a character array.

What are you trying to read from the file, and how is it arranged? There might be easier ways than using fstream::read.
Topic archived. No new replies allowed.