3 Replies - 206 Views - Last Post: 16 October 2012 - 06:44 AM Rate Topic: -----

#1 joerdie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-April 10

Cannot find file error.

Posted 16 October 2012 - 04:43 AM

Hello all, I am trying to complete this example from my C book and I am going a little crazy. i am trying to read in a file using fscanf, then manipulate the data and print it back out. (I am guessing the probable winner of a football game.) I am supposed to use pointers to pass and get this data instead of return statements. I am currently just trying to get the data that is read in to print out to make sure I am on the track but even that doesn't seem to be working. When I run the program, I get the "The system cannot find the file specified." error. The file, "input1.txt" is located in the same folder with my source file AND one level up in with the solution file. I feel like I am missing something rather simple here but am starting to pull my hair out. Any help would be greatly appreciated. Thanks!


#include <stdio.h>

// function prototypes
void getInput(FILE *, char *, char *, int *, int *, int *, int *, int *, int *, int *, int *, int *); 
void output(FILE *, char *, char *, int *, int *, int *, int *, int *, int *, int *, int *, int *);

main()
{
    char HT[20], VT[20];
    int HToffense, HTdefense, HTspecialteams, HThomestrength, HTcrowd, VToffense, VTdefense, VTspecialteams, VTroadstrength;
    FILE *fp;

    // open files
    fp=fopen("input1.txt", "r");
    
	while (!feof(fp))    {
        getInput(fp, HT, VT, &HToffense, &HTdefense, &HTspecialteams, &HThomestrength, &HTcrowd, &VToffense, &VTdefense, &VTspecialteams, &VTroadstrength);
        output(fp, HT, VT, &HToffense, &HTdefense, &HTspecialteams, &HThomestrength, &HTcrowd, &VToffense, &VTdefense, &VTspecialteams, &VTroadstrength);
    }
    // close files
    fclose(fp);
}

void getInput(FILE *fp, char *HT, char *VT, int *HTo, int *HTd, int *HTs, int *HTh, int *HTc, int *VTo, int *VTd, int *VTs, int *VTr)
{
    fscanf(fp, "%s %s %d %d %d %d %d %d %d %d %d", HT, VT, HTo, HTd, HTs, HTh, HTc, VTo, VTd, VTs, VTr);
}

void output(FILE *fp, char *HT, char *VT, int *HTo, int *HTd, int *HTs, int *HTh, int *HTc, int *VTo, int *VTd, int *VTs, int *VTr)
{
    fprintf(fp, "%s %s %d %d %d %d %d %d %d %d %d", HT, VT, HTo, HTd, HTs, HTh, HTc, VTo, VTd, VTs, VTr);
}


This post has been edited by joerdie: 16 October 2012 - 04:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot find file error.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: Cannot find file error.

Posted 16 October 2012 - 05:25 AM

What are you using, Visual C++?

Most IDEs have a setting/concept of a "working directory", and this will be the starting directory when the program is started within the IDE. Your file should be in that directory.
Was This Post Helpful? 0
  • +
  • -

#3 joerdie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-April 10

Re: Cannot find file error.

Posted 16 October 2012 - 05:49 AM

View PostJackOfAllTrades, on 16 October 2012 - 05:25 AM, said:

What are you using, Visual C++?

Most IDEs have a setting/concept of a "working directory", and this will be the starting directory when the program is started within the IDE. Your file should be in that directory.


I am using Visual Studio C++. The .txt file is in the folder with the code. In the past, I have placed .txt files in this folder and have not had this issue. I am wondering if I am missing something in my code however because I have done similar things before using scanf.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: Cannot find file error.

Posted 16 October 2012 - 06:44 AM

If you are using Visual C the "working" directory can be different depending upon the version of your IDE, it is either where the project files are located or where the .exe is located. I am not familiar with this compiler but I believe that there is a setting that allows you to specify the "working" directory.

Another way to determine the default directory for your system is to create an output file using a unique name. Then after running the program use your operating system's find functionality to find where this unique file is located, that will be your "working" directory.


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1