OPENING A TEXT FILE

We have been assigned a task where we have to open a text file but we do not know how to do it.

Does anyone know how??
Yes, i am struggling with this as well.
Isn't that a great shame :(
Thanks a lot MiiNiPaa, however I also need to know how to display the text file and store it as a string :)
Display it how? On standard output?

Those tutorials show you how to read from a file into a string.

//NOTE IM ALSO BEGINNER LOL...
//How many lines of words are in the file ?? leequayle

#include <iostream>
#inlcude <fstream>
#include <string>

using namespace std;

int main()
{
ifstream infile; //Container for the file
infile.open("nameOfTheFile.txt"); //Name of the file you want to open
string stringFromFile; //string variables to store extracted strings


if(infile.fail()) //Display error if the file failed to open
{
cout<<"Input file failed to open";
}
else
{
getline(infile, stringFromFile);
}
cout<<stringFromFile

return 0;
}

















Last edited on
Gelier, there is 44 words in the text file :)
i am slightly confused with the text file to i researched it and it can up with this.
-----------------------------------------------------------------------------------------
#define CHUNK 1024 /* read 1024 bytes at a time */
char buf[CHUNK];
FILE *file;
size_t nread;

file = fopen("test.txt", "r");
if (file) {
while ((nread = fread(buf, 1, sizeof buf, file)) > 0)
fwrite(buf, 1, nread, stdout);
if (ferror(file)) {
/* deal with error */
}
fclose(file);
}
----------------------------------------------------------------------------------------------------

method above is essentially how you will read a file with a dynamically allocated array

thats all i can find lee



Last edited on
Hmm, what we want, is to open a text file, then display it preferably using std::cout (cout) and perhaps convert it to ASCII values.
what we want, is to open a text file, then display it preferably using std::cout (cout)
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <fstream>


int main()
{
    std::ifstream in("A-small-practice.in");
    std::cout << in.rdbuf();
}
//Ok leequayle, this shoud work for your 44 lines just using for loop and a string.


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//Don't forget to display error if the input file does not open, as I did on my first comment
//so you would know what's happening if if doesn't display anything on the command //prompt.

int main()
{
ifstream infile;
infile.open("listOfWords.txt"); //open file

for(string listOfWords; getline(infile, listOfWords, '.'); ) //read sentences including
//spaces

cout<<listOfWords; //Display sentences

return 0;
}



I hope this helps ! MiiNiPaa how do you do those progamming text ?
Last edited on
You mean this?


This is quote tags " sign in format panel next to message box

code tags <>

1
2
3
#include code tags or qoute tags or other tags

cout << a short tutorial on using the message box in the forum !? << '\n';





--------------------------------------------------------------------------------------------->

click reply and follow the arrow.
Last edited on
It depends what tool you're using to create the c++ code, if you're using MS Visual Studio then you should use the StreamWriter and StreamReader classes:

1
2
3
4
5
6
7
8
9
10
11
12
// Creating a stream reader object and assigning the "hello.txt" text file to it
StreamReader readText = new StreamReader("hello.txt");

// Assigning the value in the text file to a string variable using the ReadLine() function
string helloText = readText.ReadLine();

// Creating a new stream writer object and assigning the "hello.txt" text file to it
StreamWriter editText = new StreamWriter("hello.txt");

// Using the WriteLine() function to add the text "hello everybody" to the original text file
editText.WriteLine("hello everybody");


You should have the System.IO namespace if you want to use the StreamWriter and StreamReader functions (which you may already have). If you're just using pure C++ then use the iostream functions described in the comments above.
We are using MS Visual, which folders do we need to include?
Last edited on
Any helpers?
@softwaretime
No. That is .Net and CLI. It'd look like this:

StreamReader^ sr = gcnew StreamReader( "TestFile.txt" ); // Note: ^ and gcnew for the non C++ pointer

@willfarrant
What help?
Topic archived. No new replies allowed.