Thread: help with writing to file please

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    help with writing to file please

    Hi, I have this code in an if statement inside a mousepressed event and it doesnt appear to be overwriting the current file (variables.txt) with the new string

    Code:
    			if (i==2)
    					{
        char my_filename[] = "variables.txt";
    					fopen(my_filename,"w");
    printf(my_filename,"%d\n",2);
    					}
    Any help appreciated,
    thanks,
    Marc

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    printf goes to stdout, you can't specify the file. You mean fprintf. By the way, you should check that fopen succeeded.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Thanks, how do i check that the file has been opened?

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    p.s changing to fprintf gives a couple of warnings...'parsing argument 1 of printf from incompatible pointer type'

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You pass the file handle returned by fopen(..) to fprintf. fopen(..) returns a FILE* handle which you should be saving.

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Code:
    {
        char my_filename[] = "foo.txt";
        FILE *myfile;
        myfile = fopen(my_filename, "w");
        if (myfile == NULL)
        {
           /* error message and appropriate action (exit, etc.) */
        }
        fprintf(myfile, "%d\n", 2);
        /* ... */
    }

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Thanks, I have this now... but I'm not getting any print error output or changed text in the variables.txt file???
    Code:
    				case kEventMouseMoved:
    					kindName = "MouseMoved";
    					GetEventParameter(inEvent, kEventParamMouseLocation, 
    									  typeQDPoint, NULL, sizeof(Point), NULL, &pt);
    					fprintf(stderr, "location - %.0f, %.0f - ", x2, y2);
    					switch (i){
    					case 1:
    					x1=pt.h;
    					y=pt.v;
    					i=2;
    					break;
    					
    					case 2:
    					x2=pt.h;
    					y2=pt.v;
    					i=1;
    					xdouble=x2-x1;
    					ydouble=y2-y;
    					distance=distance+(sqrt(pow(xdouble, 2)))+(sqrt(pow(ydouble, 2)));
    					x1=x2;
    					y=y2;
    					i=2;
    					break;
    					
    					{
        char my_filename[] = "variables.txt";
        FILE *myfile;
        myfile = fopen(my_filename, "w");
        if (myfile == NULL)
        {
    	printf("ouch");
           /* error message and appropriate action (exit, etc.) */
        }
        fprintf(myfile, "%d\n", 2);
        /* ... */
    }
    					}
    					break;

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you won't by having a break; halfway through your case 2

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Hmm...this seems to be working (i think) i havent checked the output yet, but this produces an ouch print randomly...them by debugger appears, highlighted at the line:

    0x9000c280 <+0064> lhz r2,12(r16)

    here is my code again:
    Code:
    case kEventClassMouse:
    			className = "Mouse";
    			// get the type or mouse event
    			switch ( GetEventKind( inEvent ) )
    			{
    				case kEventMouseDown:
    					kindName = "MouseDown";
    					clicks=clicks+1;
    					break;
    					
    				case kEventMouseUp:
    					kindName = "MouseUp";
    					break;
    					
    				case kEventMouseMoved:
    					kindName = "MouseMoved";
    					GetEventParameter(inEvent, kEventParamMouseLocation, 
    									  typeQDPoint, NULL, sizeof(Point), NULL, &pt);
    					fprintf(stderr, "location - %.0f, %.0f - ", x2, y2);
    					switch (i){
    					case 1:
    					x1=pt.h;
    					y=pt.v;
    					i=2;
    					break;
    					
    					case 2:
    					x2=pt.h;
    					y2=pt.v;
    					i=1;
    					xdouble=x2-x1;
    					ydouble=y2-y;
    					distance=distance+(sqrt(pow(xdouble, 2)))+(sqrt(pow(ydouble, 2)));
    					x1=x2;
    					y=y2;
    					i=2;
    					
    					
    					{
        char my_filename[] = "variables.txt";
        FILE *myfile;
        myfile = fopen(my_filename, "w");
        if (myfile == NULL)
        {
    	printf("ouch");
           /* error message and appropriate action (exit, etc.) */
        }
        fprintf(myfile, "%d\n", clicks);
        /* ... */
    }
    				break;
    					}
    					break;
    					
    				case kEventMouseDragged:
    					kindName = "MouseDragged";
    					break;
    					
    				case kEventMouseWheelMoved:
    					kindName = "MouseWheel";
    					break;
    					
    				default:
    					break;
    			}
    				break;

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Scrub that, i think i fixed it by closing the file. I tried opening the file again to read the string it just printed. The output i get is...

    "B-P\306g\232\316}\277\377\363pevent class = Mouse"
    I have no idea what this is lol its certainly not what i want.
    I also opened the file variables.txt and 28 was stored. and sometimes nothing is stored.... :-S

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Fixed I think...however the text file resides in a folder seperate to where the application is...
    Ah well as long as it works.

    Could anyone please explain how to read two strings instead of the one now please, say for example if they were on different lines in the text file?

    234567 //string1
    101010 //string2

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while happy return values from function reading one line
        if x == 0
            treat the as string 1 when you do something with it
            x = 1
        else
            treat this as string 2 when you do something with it
            x = 0
    There are plenty of methods. Some better than others. All worth experimenting with.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM