fscanf problem

Discussion in 'Mac Programming' started by TheDoubleEm, Feb 4, 2010.

  1. TheDoubleEm macrumors newbie

    TheDoubleEm

    Joined:
    Jun 23, 2009
    #1
    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!
     
  2. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #2
    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?
     
  3. TheDoubleEm thread starter macrumors newbie

    TheDoubleEm

    Joined:
    Jun 23, 2009
    #3
    yes, Visual Basic...I just copied code :)
    so it's same code
     
  4. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #4
    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).
     
  5. TheDoubleEm thread starter macrumors newbie

    TheDoubleEm

    Joined:
    Jun 23, 2009
    #5
    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;
    }
    
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    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.
     
  8. TheDoubleEm thread starter macrumors newbie

    TheDoubleEm

    Joined:
    Jun 23, 2009
    #8
    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!! :)
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    I suggest this command, entered after the cd to get to specific directory:
    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
    
     
  10. TheDoubleEm thread starter macrumors newbie

    TheDoubleEm

    Joined:
    Jun 23, 2009
    #10
    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!
     
  11. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #11
    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!
     
  12. TheDoubleEm thread starter macrumors newbie

    TheDoubleEm

    Joined:
    Jun 23, 2009
    #12
    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)?
     
  13. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #13
    I suggest reading the man page for gets:
    Code:
    man gets
    
    Xcode also provides access to man pages, under its Help menu.


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


    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
     

Share This Page