C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-11-2006, 10:40 AM   #1
Registered User
 
stevespai's Avatar
 
Join Date: Jun 2006
Posts: 23
Problem with opening file and pathname

Hi All,

I'm new to the boards. I have a problem opening a file for reading. Here are snipets of code I am using.

Code:
 /// <summary> 
    /// Create a new reader for the given stream. 
    /// </summary> 
    /// <param name="s">The stream to read the CSV from.</param> 
    public CSVReader(Stream s) : this(s, null) { } 

    /// <summary> 
    /// Create a new reader for the given stream and encoding. 
    /// </summary> 
    /// <param name="s">The stream to read the CSV from.</param> 
    /// <param name="enc">The encoding used.</param> 
    public CSVReader(Stream s, Encoding enc) { 

      this.stream = s; 
      if (!s.CanRead) { 
        throw new CSVReaderException("Could not read the given CSV stream!"); 
      } 
      reader = (enc != null) ? new StreamReader(s, enc) : new StreamReader(s); 
    } 

    /// <summary> 
    /// Creates a new reader for the given text file path. 
    /// </summary> 
    /// <param name="filename">The name of the file to be read.</param> 
    public CSVReader(string filename) : this(filename, null) { } 

    /// <summary> 
    /// Creates a new reader for the given text file path and encoding. 
    /// </summary> 
    /// <param name="filename">The name of the file to be read.</param> 
    /// <param name="enc">The encoding used.</param> 
    public CSVReader(string filename, Encoding enc) 
      : this(new FileStream(filename, FileMode.Open), enc) { }
Here is where I am trying to open the file:

Code:
using ( CSVReader csv = new CSVReader(@"C:\Documents and Settings\testfile1.csv") )
Now, I want to be able to change the pathname so that my program can process multiple files (using a for loop).

ie:
"C:\Documents and Settings\testfile1.csv"
"C:\Documents and Settings\testfile2.csv"
"C:\Documents and Settings\testfile3.csv"
...


I tried loading the path into a string variable and using String.Concat( , ) to create the full path. However, I get errors from the '\' within the strings.

Does anyone know anyother way to accomplish what I am trying to do?

Thanks,
Steve
stevespai is offline   Reply With Quote
Old 07-11-2006, 11:01 AM   #2
pwns nooblars
 
Join Date: Oct 2005
Location: Portland, Or
Posts: 1,094
"\\" =='\'
__________________
Enlighten Yourself

I'm back!
Wraithan is offline   Reply With Quote
Old 07-12-2006, 07:33 AM   #3
Anti-Poster
 
Join Date: Feb 2002
Posts: 1,241
Quote:
Originally Posted by Wraithan
"\\" =='\'
Or you could use C#.
Code:
string.Format(@"C:\folder\folder\filename{0}.txt",1);
__________________
Rule #1: Every rule has exceptions
pianorain is offline   Reply With Quote
Old 07-13-2006, 08:03 AM   #4
Registered User
 
Join Date: Jun 2006
Posts: 2
Look at the MSDN docs on System.IO.Path, specifically System.IO.Path.Combine(string, string)

Don't reinvent the wheel or brute force things when there are already framework methods to do the work for you.

-Casey
caseymanus is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with readlink()? QuietBoi C Programming 15 04-08-2007 05:55 AM
Bitmap Displaying Problem MadCow257 Game Programming 0 01-25-2005 05:21 PM
getting filenames madsmile C++ Programming 4 03-12-2002 02:40 PM
header file compile error Unregistered C Programming 5 02-23-2002 06:28 AM
file pointers + recursion = sore head korbitz C Programming 4 12-18-2001 05:55 PM


All times are GMT -6. The time now is 03:22 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22