answersLogoWhite

0


Best Answer

The following function will read any valid file name one byte at a time.

int read_file(char* filename)

{

FILE* input=fopen( filename,"rb" );

if( !input )

{

std::cerr<<"File access error: "<<filename<<std::endl;

return( -1 );

}

char ch;

while( fread( &ch, 1, 1, input )==1 )

{

// process the byte...

}

fclose( input );

return( 0 );

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus code to read the data from any file?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is meant by read and write txt in file handling?

read: moving data from file to memory write: moving data from memory to file


Why would you need to convert a string to a numeric data type?

For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.


Which of the following types of memory contain data that cannot be modified by the user?

Pick one: ROM, PROM, EPROM write-protected magnetic disk/tape, CD-ROM, DVD-R write-protected partition/file, other user's or sysadmin's file code-segment, read-only data-segment, other user's or kernel's code- or data-segment


What is the difference between File Reader and FileInputStream in java?

FileReader used to read the character stream in the file.i.e a file that contanis only the character means FileReader is the choice to read the file.On the other hand if the file contains image,byte like raw data format means FileInputStream is the choice to read the data in the file,.


What is file mode in c plus plus?

There are 6 main types of file opening mode:* "r". Open file for reading and file must exist; * "w" Open file for writing. If file does not exist it is created or if life already exist it's content is erased. * "a" Open file for appending. It adds all information at the end of the file leaving old data untouched. If file does not exist it is created. * "r+" Open file for reading and writing and file must exist. * "w+" Open file for writing and reading. If file does not exist it is created or if life already exist it's content is erased. * "a+" Open file for appending and reading. Again all new data is written at the end of the file old data leaving untouched. If file does not exist it is created. (You can read old data by moving pointer in file using fseek or rewind functions from stdio.h. But all writing operations will be done at the end of the file no matter how you change pointer) It is assumed by default that file will be standard ASCII text file in order to open file as binary file, you need to add "b" indicator:FILE *myFile = fopen("myfile.txt", "wb");/ * following two has identical meaning */FILE *myFile = fopen("myfile.txt", "w+b");FILE *myFile = fopen("myfile.txt", "wb+");

Related questions

What is meant by read and write txt in file handling?

read: moving data from file to memory write: moving data from memory to file


When reading data in a data file the data will be read?

in order it was written.


What is function of End-of-file?

It means 'no more data to read in this file'.


When you copy data from a file on a storage device into RAM you?

read to the file


Why would you need to convert a string to a numeric data type?

For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.For example, to interpret user input; to read data from a text file or from some other program that produces the data as a string.


Which of the following types of memory contain data that cannot be modified by the user?

Pick one: ROM, PROM, EPROM write-protected magnetic disk/tape, CD-ROM, DVD-R write-protected partition/file, other user's or sysadmin's file code-segment, read-only data-segment, other user's or kernel's code- or data-segment


Can you write a program to read a set of scores from a file and compute the average and print it on the screenby c plus plus?

Yes. Use cin and/or getline to read the formatted data into an array, compute the average then output the result using cout.


Traditional file environment?

Traditional file environment is not well suited to save data in them. They make it difficult to read the data as they are more unstructed.


How to verify the ntfs and folder permissions?

NTFS File Permission Allows the User To Full Control Change permissions and take ownership, plus perform the actions permitted by all other NTFS file permissions Modify Modify and delete the file plus perform the actions permitted by the Write permission and the Read & Execute permission Read & Execute Run applications plus perform the actions permitted by the Read permission Read Read the file, and view file attributes, ownership, and permissions Write Overwrite the file, change file attributes, and view file ownership and permissions


How do you open a data file?

You can use fstream header file to read and write in files. It has two main components ifstream: stream which you can use to read data, and ofstream which allows you to save in files. Use something like this:#include#includeusing std::ifstream;using std::cout;using std::endl;int main(){char fileName[] = "myfile.txt";//file to read fromifstream inData;//stream to read from myfile.txtint data = 0;//to read one number of type int (we assume that the file contains it)inData.open(fileName);//connect the stream to the fileif (inData.fail())//if coult not open the file, program will be closed{cout data;//read one number from the file and save it in data of type intcout


What are read only files?

Files that can only be read (data can be retirieved) are called read-only files. You cannot write any data into a read-only file (you cannot edit it).


How do you read hyper terminal data to a file?

I have the same problem............ I want to read and write file but using Xmodem..... Please help me out as soon as possible