here is the question:
5. Write a basic program that allows the user to write names to a text file and read from the text file and displaying them on the screen.

string filename;
StreamReader outputFile = new StreamReader("c:\\PriceList.txt");

// Read the first item.
filename = outputFile.ReadLine();
// If an item was read, display it and read the remaining items.
while (filename != null)
{
    Console.WriteLine(filename);
    filename = outputFile.ReadLine();
}
outputFile.Close();

shows up error!!! dont knwo what to dio!! any clues???

Edited 7 Years Ago by memory100: aded extar info

hmm i had a look. - if this was c++ i would try this.

#include "stdafx.h"
#include <iostream> // This libary allows standard input and output operations. 
#include <string> // strings are used as tempory storage - for the input to a text based file
#include <vector> // at one point in my program i expermented with vectors for data storage
using namespace std; // (or maybe std::getline;, std:: string;, std:: cout; , std:: cin; (namespace howecver is a short hand version to reduce code and possible errors. 
#include <fstream> // this allows filestreaming and allows the reading and writing of a file

int AddFirstName(int intEnterANumber)
{
cout << "Please Enter the FirstName \n";
cin >> strFirstName;

ofstream File;

File.open("Store.txt",ios::app);

File << strFirstName;
File <<",";

File.close();

File.open("StoreTheFirstName.txt",ios::app);

File << strFirstName;

File.close();

return 0;
}

sorry if this does not help. but c# is kinda depritive of c++.

Edited 7 Years Ago by __avd: Use [code] tags to wrap your code.

here is the question:
5. Write a basic program that allows the user to write names to a text file and read from the text file and displaying them on the screen.

string filename;
StreamReader outputFile = new StreamReader("c:\\PriceList.txt");

// Read the first item.
filename = outputFile.ReadLine();
// If an item was read, display it and read the remaining items.
while (filename != null)
{
    Console.WriteLine(filename);
    filename = outputFile.ReadLine();
}
outputFile.Close();

shows up error!!! dont knwo what to dio!! any clues???

Try this:

try
{
string filename;
StreamReader outputFile = new StreamReader("c:\\PriceList.txt");
while(!outputFile.EndOfStream)
{
    filename = output.ReadLine();
    Console.WriteLine(filename);
}
outputFile.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

If this dosn't work, please post the exact exception that occurs (if my syntax is correct it should output to the Console). I did not have time to test this code. Please let me know if it doesn't work and I'll look into it further.

but wen i read path from a text file, den it reads & shows correctly but anable to store the path of file into string type variable.
The first file contains the paths of all files used in project, so i wnt to open this file & read path of a file den store into variable for open this file.
please help me .............
Thanks in advance

@memory100 - did you resolve your issue? if not, what error are you recieving?

@kool.net - please dont piggyback onto threads. If you ahve a problem you can create a thread and get help specific to your problem. Adding new problems to related threads leads to confusion.

This article has been dead for over six months. Start a new discussion instead.