Thread: A problem in my fileopener

  1. #1
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790

    A problem in my fileopener

    I am having a problem. How can I set up a program so that a user can enter what file he wants to read, including the path.

    Code:
    #include <stdio.h>
    #include <fstream.h>
    #include <string>
    
    int main(void)
    {
    char szInputString[1000];
      char szBuffer[10000];
      FILE *input_file, *out_file = stdout;
    
    
      cout << "please enter the text file you would like to read: \n";
      cin >> szInputString;
      if ((input_file = fopen(szInputString, "r"))==NULL) {
        return 1;
      }
      while ((fgets (szBuffer, sizeof szBuffer, input_file)) != 0)
      {
    
    	  fprintf (out_file, " \n %s", szBuffer);
      }
    	  fclose (input_file);
      
      return 0;
    }
    with the code above, I cant even open it from diff directories.
    Last edited by EvBladeRunnervE; 02-03-2003 at 11:30 PM.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    You're writing in C++ or C? (cin and cout) are C++
    Anyway, what is your input? when you write the path, he exit the program, or just the fgets() part aren't working well?

  3. #3
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    say, if the use inputs a name that is in the directory by just saying "untitled.txt" for example, it loads up. what I want someone to be able to do is say, type "C:\programming files\untitled.txt" which currently now ends the program.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    try double slashes on the input:
    Code:
    C:\\file.txt
    Or, try like this too:
    Code:
    C:/file.txt
    I don't know about "cin", if he add's something to the string, like fgets() and '/n' at the end of the file.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use this to find out why your file isn't opening:

    >>perror(szInputString);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM