Silly Visual C++ Problem

This is a discussion on Silly Visual C++ Problem within the C Programming forums, part of the General Programming Boards category; Hello, I recently got Visual C++ and I am having a problem with it. I have a text file I ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    22

    Silly Visual C++ Problem

    Hello, I recently got Visual C++ and I am having a problem with it. I have a text file I want to input into my program but I don't know the directory Visual C++ reads files from or how to find out. I'm sure it's obvious but I can't figure it out.

    I also have another problem. Before I got Visual C++ I had some trouble with taking input from a file and putting it into an array. The text file consisted of only 1's and 0's with no whitespace or any other characters. Here is the code segment:

    Code:
    #include <stdio.h>
    #define LENGTH 2000
    FILE *fp;
    int output[LENGTH];
    
    ...
    
    int temp;
    int index = 0;
    
       if((fp = fopen("test.txt", "r")) == NULL) {
          printf("Error opening file.");
          exit(1);
       }   
       
       for (;;) {
           if ((temp = fgetc(fp)) == EOF) break;
           if (temp) test[index] = 1;
           else test[index] = 0;
           printf("%d %d\n" , index, test[index]);
           if ((++index) > LENGTH-1) break;        
       }
       
       fclose(fp);
    I tried a few fixes and ended up with some unexpected outputs but I can't seem to get it to work.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I have a text file I want to input into my program but I don't know the directory Visual C++ reads files from or how to find out. I'm sure it's obvious but I can't figure it out.
    When you create a "Project", you give it a directory, say D. When you build the project, it will create a subfolder, depending on what type of build you do. If you do a Debug build, it will create D/debug. If you do a Release build, it will create D/release. This is where your actual ".exe", ".lib", ".dll", etc., files will be created. This is also the default "current working directory" that the program will start in. So, put whatever files you need to read/write from/to in the debug or release folder, depending on what type of build you do.

    Alternatively, I'm pretty sure there is a way to change the working directory of the program when you run/debug it. Look around in the "Project Properties", etc., windows, or just search the documentation for it.

    As for your other problem
    I tried a few fixes and ended up with some unexpected outputs but I can't seem to get it to work.
    Explain precisely what your "unexpected outputs" are. How is "test" declared? It isn't shown in your above code.

    Code:
           if (temp) test[index] = 1;
           else test[index] = 0;
    I imagine what you want the above code to do something like
    Code:
    if ( temp is the character '1' )
      set test[index] to 1
    otherwise
      set test[index] to 0
    but that's not what its doing. It's doing this
    Code:
    if ( temp is any character besides NULL character '\0' )
      set test[index] to 1
    otherwise
      set test[index] to 0
    To fix it, be more explicit in your if condition, i.e. dont just do "if (test)".
    Last edited by nadroj; 03-11-2010 at 06:52 PM.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    That's great. I've located the appropriate directory, and now my file operation works. Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual studio.net C++ Problem
    By Dina in forum C Programming
    Replies: 5
    Last Post: 07-19-2008, 03:34 PM
  2. Visual C++ Tool Problem
    By wolfban in forum Tech Board
    Replies: 2
    Last Post: 07-23-2005, 08:41 PM
  3. errors in my class....
    By o0obruceleeo0o in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2003, 04:22 AM
  4. Visual Basic Adodc Problem
    By rahat in forum Windows Programming
    Replies: 1
    Last Post: 01-20-2002, 06:55 AM
  5. Microsoft Visual C++ compiler, cast problem?
    By jonnie75 in forum C Programming
    Replies: 5
    Last Post: 11-10-2001, 08:53 AM

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