Hi guys, I have this input.txt file and I have to scan it to get information for my project.

the input.txt file has format as:

33 56 34.8 N
118 24 30.1 W
37 40 23.5 S
144 50 54.1 E

So I used fscanf_s to scan the file. Here is my code

int long1deg,long1min,long1sec,lat1deg,lat1min,lat1sec;
	int long2deg,long2min,long2sec,lat2deg,lat2min,lat2sec;
	char orien1lon[2], orien1lat[2],orien2lon[2],orien2lat[2];


     
	FILE *f;
	fopen_s(&f, "input.txt", "r");

    if (f==NULL)
    perror ("Error opening file");
    else{
    while(feof(f)== 0){
     //Long of point 1
    fscanf_s(f,"%d",&long1deg);
    fscanf_s(f,"%d",&long1min);
    fscanf_s(f,"%d",&long1sec);
    fscanf_s(f,"%s",orien1lon);
	// Lat of point 1
    fscanf_s(f,"%d",&lat1deg);
    fscanf_s(f,"%d",&lat1min);
	fscanf_s(f,"%d",&lat1sec);
	fscanf_s(f,"%s",orien1lat);
	//Long of point 2
	fscanf_s(f,"%d",&long2deg);
	fscanf_s(f,"%d",&long2min);
	fscanf_s(f,"%d",&long2sec);
	fscanf_s(f,"%s",orien2lon);
	//Lat of point 2
	fscanf_s(f,"%d",&lat2deg);
	fscanf_s(f,"%d",&lat2min);
	fscanf_s(f,"%d",&lat2sec);
	fscanf_s(f,"%s",orien2lat);
    }
    fclose(f);
    }

But I always have this problem when I compile it: "Error opening file: No such file or directory".

Is there a specific location where I have to put my input.txt file? I suspect visual studio cannot find my text file. I opened the text file while compile my c++ file.
Thank you in advance.

Try it without the bastardized *_s functions. Just use fscanf() and fopen() . They are standard and work in all compilers.
Be sure to check the syntax of fopen() because it's different than your use of fopen_s()

> Is there a specific location where I have to put my input.txt file?

Either place input.txt in the working directory of your program.

Or use a complete path like "C:\\some_dir\\etc\\etc\\input.txt" instead of a relative patch.

how do I use the complete path like you said above, vijayan121?

So I tried to use fopen instead of fopen_s. but I got error message:
http://imageshack.us/photo/my-images/402/capture1kh.jpg/

Here is my code:

int long1deg=0,long1min=0,long1sec=0;
	int lat1deg=0,lat1min=0,lat1sec=0;
	int long2deg=0,long2min=0,long2sec=0;
	int lat2deg=0,lat2min=0,lat2sec=0;
	char orien1lon[2], orien1lat[2],orien2lon[2],orien2lat[2];


     
	FILE*f = fopen( "input.txt", "r");

	if (f!=NULL)
	{
    perror ("Error opening file");
	fclose(f);
	}
    else{
    while(feof(f)== 0){
     //Long of point 1
    fscanf_s(f,"%d",&long1deg);
    fscanf_s(f,"%d",&long1min);
    fscanf_s(f,"%d",&long1sec);
    fscanf_s(f,"%s",orien1lon);
	// Lat of point 1
    fscanf_s(f,"%d",&lat1deg);
    fscanf_s(f,"%d",&lat1min);
	fscanf_s(f,"%d",&lat1sec);
	fscanf_s(f,"%s",orien1lat);
	//Long of point 2
	fscanf_s(f,"%d",&long2deg);
	fscanf_s(f,"%d",&long2min);
	fscanf_s(f,"%d",&long2sec);
	fscanf_s(f,"%s",orien2lon);
	//Lat of point 2
	fscanf_s(f,"%d",&lat2deg);
	fscanf_s(f,"%d",&lat2min);
	fscanf_s(f,"%d",&lat2sec);
	fscanf_s(f,"%s",orien2lat);
    }
    fclose(f);
    }

ah, thanks. It does accept the file now.
I have another question.
So when I write

fscanf_s(f,"%s",orien1lon);

this means orien1lon contains a string of multiple characters. However, looking at my text file, it only contains a single let, either N,S,W, or E. should I change the format into character for orien1lon?
Should I write like this?

fscanf_s(f,"%c",orien1lon);

As long as that part of the input will always be a single character, yes, you will be able to do that.

Also, if you want to make your code shorter, I believe fscanf returns the number of arguments filled, so you could fill multiple data element with a single call to fscanf().

The third argument you're using looks like it has a decimal point, also, so you should probably use float to avoid headaches.

if (fscanf(f, "%d %d %f %c", &long1deg, &long1min, &long1sec, &orien1lon) == 4)
{
  //Success
}
else
{
  //ERROR!
}

ah that's a very nice idea. Thank you so much for your input, Tumlee.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.