Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TheDoubleEm

macrumors newbie
Original poster
Jun 23, 2009
20
0
hello
I compiled app in c++ with VB, that scans .rtf file for degrees,minutes and seconds, converts them into radians and decimal degrees and then prints results in another .rtf file...

data in input .rtf file looks lke this(degrees,minutes,seconds):
59 49 39

it all works fine under VB and windows cmd...but when I try to run it through terminal (with created a.out), it tells me, that my scanned angle was 32767 1606420320 0.00

so problem is obviously in scan function or the .rtf itself...
line from .cpp where the scan func is looks like this:
fscanf(inputfile,"%d %d %lf",&degrees,&minutes,&seconds)!=EOF
printf("\n-> %10.2d %10.2d %10.2f\n", degrees,minutes,seconds);


what could be wrong?

thanks for help!
 
Wait, what's VB? Visual Basic? Did you convert a (Windows) Visual Basic program to C++ on the Mac, or is it the same C++ code you're running on both platforms?
 
did you initialize the variables?

Did you copy the .rtf file from the PC?

I would just post the code (put it between the
Code:
 and [\code] brackets).
 
rtf file is exactly the same (tried also with .txt)

Code:
#include<stdio.h>
#include<stdlib.h>

#define pi 3.1415926536

void EndApp(void);
void Title();
double DMS2DEG(int degrees, int minutes, double seconds);
double DMS2RAD(int degrees, int minutes, double seconds);

int main()
{
	char NameInput[20],NameOutput[20]; 
	int degrees, minutes;
	double seconds;
	FILE *input, *output;
	int counter=1;
	Title();
	
	printf("Specify name of input file  :");
	gets(NameInput);
	printf("Specify name of output file :");
	gets(NameOutput);
	
	input=fopen(NameInput,"r");
	if (input == NULL){
		printf("\n\nFile %s cannot be opened!\n",NameInput);
		fflush(stdin);
		getchar();
		exit(1);
	}
	output=fopen(Nameoutput,"w");
	
	while(fscanf(input,"%d %d %lf",&degrees,&minutes,&seconds)!=EOF)
	{
		printf("---------------------------------------");
		printf("\n-> %10.2d %10.2d %10.2f\n", degrees,minutes,seconds);
		
		if(degrees>359 || minutes>59 || seconds>59 || minutes<0 || seconds<0)
		{
			printf("Angle isn't logical...");
			EndApp();
		}
		
		double res1 = DMS2DEG(degrees,minutes,seconds);
		printf("\nAngle is %f decimal degrees.\n",res1); 
		
		fprintf(output,"\nTASK:%3d\n",counter++);
		fprintf(output,"-----------\n\n");
		fprintf(output,"RESULTS:\n");
		fprintf(output,"Angle is %f  decimal degrees.\n",res1);
		
		double res2 = DMS2RAD(degrees,minutes,seconds);
		printf("\nAngle is %f radians.\n",res2); 
		
		fprintf(izhod,"Angle is %f radians.\n",res2); 
	}
	
	fclose(input);
	fclose(output);
	EndApp();
	return 0;
}

void EndApp(void)
{
	fflush(stdin);
	printf("\nPress ENTER ...");
	getchar();
}

void Title()
{
	puts("App for conversion");
	printf("\n\n");
}

double DMS2DEG(int degrees, int minutes, double seconds)
{
	double DEG;
	if(degrees<0)
	{
		degrees=degrees+360;
	}
	DEG = degrees + ((minutes + (seconds / 60.0)) / 60.0);
	return DEG;
}

double DMS2RAD(int degrees, int minutes, double seconds)
{
	double DEG,RAD;
	if(degrees<0)
	{
		degrees=degrees+360;
	}
	DEG = degrees + ((minutes + (seconds / 60.0)) / 60.0);
	RAD = DEG * (pi/180);
	return RAD;
}
 
I would just post the code (put it between the
Code:
 and [\code] brackets).[/QUOTE]

Post the rtf file, too.

If it's short, just put it between CODE tags.  If it's long, then zip it and attach it.

Or post code and rtf to someplace like pastebin.com, then post the URL here.

And also post the exact command-line you used to compile the C++ source file.
 
<source code>
I saved the posted source as degrees.c. I entered this command:
Code:
gcc degrees.c

Its output is:
Code:
degrees.c: In function 'main':
degrees.c:31: error: 'Nameoutput' undeclared (first use in this function)
degrees.c:31: error: (Each undeclared identifier is reported only once
degrees.c:31: error: for each function it appears in.)
degrees.c:55: error: 'izhod' undeclared (first use in this function)

So whatever you compiled, it clearly isn't this exact source code.


EDIT: Results after correcting errors in source.

file: dms.txt
Code:
0 0 0
59 49 39

output from running program:
Code:
App for conversion


warning: this program uses gets(), which is unsafe.
Specify name of input file  :dms.txt
Specify name of output file :out.txt
---------------------------------------
->         00         00       0.00

Angle is 0.000000 decimal degrees.

Angle is 0.000000 radians.
---------------------------------------
->         59         49      39.00

Angle is 59.827500 decimal degrees.

Angle is 1.044187 radians.

Press ENTER ...

My tentative conclusion: the program works, but you're giving it bad data.

My first guess is that you should give it a plain text file, which is what I did. Giving it an rtf file makes no sense, because the posted code does nothing at all to parse, identify, or remove any of the RTF formatting data.

For example, when I convert "dms.txt" to Rich Text using TextEdit, and save as RTF, the resulting file contains the following RTF:
Code:
{\rtf1\mac\ansicpg10000\cocoartf824\cocoasubrtf480
{\fonttbl\f0\fswiss\fcharset77 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww14000\viewh11200\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural

\f0\fs28 \cf0 0 0 0\
59 49 39\
\
}
There is no possible way this RTF will be correctly parsed by the posted code.
 
rtf file contains only this:
59 49 39

well i've got to make multiple calculations for class...but that isn't the case here, as I've got problems even with one angle :)

in terminal I used:
cd to get to specific directory...
g++ degrees.cpp
to create a.out
./a.out to run it
and when it asks for input name, I specify Input.rtf (as it is saved in same folder)

EDIT
tried to create input file as plain text...and it worked
thank you very much!! :)
 
cd to get to specific directory...
g++ degrees.cpp
to create a.out
./a.out to run it
and when it asks for input name, I specify Input.rtf (as it is saved in same folder)

I suggest this command, entered after the cd to get to specific directory:
cat Input.rtf

Copy and paste the output into your reply post.


Also, I changed my source file to "degrees.cpp", and compiled it with g++ command. I get exactly the same results as I posted before.

Finally, the output file written by the program is not actually in RTF format. Just giving a file the ".rtf" extension doesn't automatically make it into the RTF format. Your posted code isn't writing RTF format. It's only writing plain text. As before, you can confirm what's in the file using the 'cat' command.

You can also dump any file in hex using this command:
Code:
hexdump -C pathToFile
 
well I certainly would use that command you suggested, but my professor demands .txt, so when I compile on Mac, plain text will do the trick

Thanks again for your effort!
 
output from running program:
Code:
App for conversion

warning: this program uses gets(), which is unsafe.

Off-topic, but I was surprised this warning was shown during running of the program, not during compilation. I just verified it here with a small test app, and indeed, the warning appears runtime. Spiffy!
 
well do you have any suggestion to replace it?

another question: where should I place this exact input text file, so I could read it when compiling in Xcode (I tried /build/debug where the executable is-but it doesn't work)?
 
well do you have any suggestion to replace it?
I suggest reading the man page for gets:
Code:
man gets

Xcode also provides access to man pages, under its Help menu.


another question: where should I place this exact input text file, so I could read it when compiling in Xcode?

Do you understand what a working directory is?
http://en.wikipedia.org/wiki/Working_directory


(I tried /build/debug where the executable is-but it doesn't work)

What doesn't work?

Do you mean you typed /build/debug/Input.txt into your program, and no file could be opened?

If so, then you should revisit the concept of pathnames, paying particular attention to what a leading slash means.
http://en.wikipedia.org/wiki/Pathname
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.