How do you open a file in C

0 votes
I want to open a file in C++ for reading.

I need to be able to accomplish that for the following reasons:

text files, which would need some kind of read line function binary files, which would allow raw data to be read into a char* buffer
Aug 8, 2022 in C++ by Nicholas
• 7,760 points
640 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Depending on your requirements, there are three options. You could call fopen/fread/fclose in C, or you could use the C++ fstream facilities (ifstream/ofstream), or if you're using MFC, you could use the CFile class, which offers methods to do actual file operations.


All of them are adequate for text and binary, but none have readline capabilities. In that instance, you'd probably use the fstream classes (fstream.h) and the stream operators ( and >>) or the read function to read/write text blocks:

int nsize = 10;

std::vector somedata(nsize);

ifstream myfile;

myfile.open("");

myfile.read(somedata.data(), nsize);

myfile.close();

It's worth noting that if you're using Visual Studio 2005 or later, conventional fstream might not be accessible (there's a new Microsoft solution that's somewhat different but does the same thing).

answered Aug 10, 2022 by Nicholas
• 7,760 points

edited Mar 5
0 votes

Depending on your requirements, there are three options. You could call fopen/fread/fclose in C, or you could use the C++ fstream facilities (ifstream/ofstream), or if you're using MFC, you could use the CFile class, which offers methods to do actual file operations.


All of them are adequate for text and binary, but none have readline capabilities. In that instance, you'd probably use the fstream classes (fstream.h) and the stream operators ( and >>) or the read function to read/write text blocks:

int nsize = 10;

std::vector somedata(nsize);

ifstream myfile;

myfile.open("");

myfile.read(somedata.data(), nsize);

myfile.close();

It's worth noting that if you're using Visual Studio 2005 or later, conventional fstream might not be accessible (there's a new Microsoft solution that's somewhat different but does the same thing).

answered Aug 10, 2022 by Nicholas
• 7,760 points

edited Mar 5

Related Questions In C++

0 votes
0 answers

How do I declare a 2d array in C++ using new?

How do I declare a two-dimensional array using new? For example, for a "typical" array, I would:      int* ary = new int[Size] but int** ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
574 views
0 votes
0 answers

How do you properly use namespaces in C++?

I come from a Java background where ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
709 views
0 votes
1 answer

How do I reverse a C++ vector?

The algorithm header has a method std::reverse for this purpose. #include <vector> #include <algorithm> int main() { std::vector<int> ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
861 views
0 votes
0 answers

How to find if a given key exists in a C++ std::map

I'm trying to check if a given ...READ MORE

Jul 14, 2022 in C++ by Nicholas
• 7,760 points
857 views
0 votes
0 answers

Storing C++ template function definitions in a .CPP file

I have some template code that I'd rather have saved in a CPP file rather than inline in the header.  I know this is possible if you know which template types will be utilised.  As an example: .h file class foo { public: template <typename ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
847 views
0 votes
0 answers

How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers

As a mathematician, one of my pet peeves of C-derived languages is : (-1) % 8 // comes out as ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
517 views
0 votes
1 answer

How to write to CSV line by line in python

The simple way of doing this will ...READ MORE

answered Nov 15, 2018 in Python by findingbugs
• 3,260 points
5,794 views
0 votes
1 answer

Open file in Python

There is this code I used for ...READ MORE

answered Nov 16, 2018 in Python by Jino
• 5,820 points
910 views
0 votes
1 answer

freopen() in python

sys.stdout is simply file object, so, you ...READ MORE

answered Jan 3, 2019 in Python by anonymous
1,731 views
0 votes
1 answer

How to append text to an existing file in Java?

Java 7+ If you just need to do ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,770 points

edited Jul 6, 2023 by Khan Sarfaraz 1,335 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP