OPENING A TEXT FILE

Mar 11, 2015 at 12:21pm
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??
Mar 11, 2015 at 12:27pm
Yes, i am struggling with this as well.
Mar 11, 2015 at 12:28pm
Isn't that a great shame :(
Mar 11, 2015 at 12:31pm
Mar 11, 2015 at 12:36pm
Thanks a lot MiiNiPaa, however I also need to know how to display the text file and store it as a string :)
Mar 11, 2015 at 6:00pm
Display it how? On standard output?

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

Mar 12, 2015 at 7:24am
//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 Mar 12, 2015 at 7:26am
Mar 12, 2015 at 9:31am
Gelier, there is 44 words in the text file :)
Mar 12, 2015 at 9:43am
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 Mar 12, 2015 at 9:43am
Mar 12, 2015 at 10:38am
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.
Mar 12, 2015 at 10:48am
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();
}
Mar 12, 2015 at 8:02pm
//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 Mar 12, 2015 at 8:17pm
Mar 13, 2015 at 1:43am
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 Mar 13, 2015 at 1:44am
Mar 14, 2015 at 9:13am
It depends what tool you're using to create the Python 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 Python then use the iostream functions described in the comments above.
Mar 19, 2015 at 9:23am
We are using MS Visual, which folders do we need to include?
Last edited on Mar 19, 2015 at 9:31am
Mar 26, 2015 at 10:19am
Any helpers?
Mar 26, 2015 at 12:30pm
@softwaretime
No. That is .Net and CLI. It'd look like this:

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

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