Thread: Problem with opening file and pathname

  1. #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

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    "\\" =='\'

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Wraithan
    "\\" =='\'
    Or you could use C#.
    Code:
    string.Format(@"C:\folder\folder\filename{0}.txt",1);
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

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