c codes need help,thanks

This is a discussion on c codes need help,thanks within the C Programming forums, part of the General Programming Boards category; The [optional other stuff] was meant to say that you may have other options you want to pass to gcc ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The [optional other stuff] was meant to say that you may have other options you want to pass to gcc - e.g. "-O3" to give you the highest level of optimization, or "-ansi" to ensure that your code is ANSI-C - there are literally hundreds of different options that gcc can be given. A text-file is not one of those. The information that your code needs to open "myfile.txt" is already in the source code.

    It also says:
    10-2.c:10: warning: empty body in an if-statement

    What do you think that means?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by matsp View Post
    The [optional other stuff] was meant to say that you may have other options you want to pass to gcc - e.g. "-O3" to give you the highest level of optimization, or "-ansi" to ensure that your code is ANSI-C - there are literally hundreds of different options that gcc can be given. A text-file is not one of those. The information that your code needs to open "myfile.txt" is already in the source code.

    It also says:
    10-2.c:10: warning: empty body in an if-statement

    What do you think that means?

    --
    Mats
    thanks for you explanation, i have to learn much more things to know that.

    i don't know what that warning means, does is mean the if-statement is not correct or in vain?

    the c textbook has that statement exactly.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hth373737 View Post
    thanks for you explanation, i have to learn much more things to know that.

    i don't know what that warning means, does is mean the if-statement is not correct or in vain?

    the c textbook has that statement exactly.
    Really?!?! Are you sure it has the semicolon before the brace?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> i don't know what that warning means, does is mean the if-statement is not correct or in vain?

    In vain, I believe.

    >> the c textbook has that statement exactly.

    Nonetheless, you should know what effect a semicolen has there.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by Sebastiani View Post
    >> i don't know what that warning means, does is mean the if-statement is not correct or in vain?

    In vain, I believe.

    >> the c textbook has that statement exactly.

    Nonetheless, you should know what effect a semicolen has there.
    Quote Originally Posted by matsp View Post
    Really?!?! Are you sure it has the semicolon before the brace?

    --
    Mats
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
    	FILE *fp,*fw;// declaring file pointers.
    	double val;
    	char ch;
    	int new;        // new for integer part.
    	if((fp = fopen("myfile.txt","r"))==NULL)
    	{
    	printf("sorry,cannot open the file\n");
    	exit(0);
    	}
        fw = fopen("mynewfile.txt","w");
    	do{
    		fscanf(fp, "%lf", &val); //read real number from the file.
    		ch = getc(fp);           //get the following number after the realnumber.
    		new=val;                 //pass numbers to integer form.cos new is defined intefer
    		fprintf(fw,"%d",new);
    		printf("%lf",val);    //print out the real number.
    		printf("%d",new);      //print out the integer number.
    		
    		if ( ch=='\n' || ch==EOF )//determin to end the progress if there is a '\n' char or End Of File.
    			printf("\n");  // switch to next line.
    	}while ( ch != EOF );
    	fclose(fp);
    	return 0;
    }
    here is the exact codes, anything wrong? i cannot find it...

  6. #21
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
    	FILE *fp,*fw;// declaring file pointers.
    	double val;
    	char ch;
    	int new;        // new for integer part.
    	if((fp = fopen("myfile.txt","r"))==NULL)
    	{
    	printf("sorry,cannot open the file\n");
    	exit(0);
    	}
        fw = fopen("mynewfile.txt","w"); /* not checking return value */
    	do{
    		fscanf(fp, "&#37;lf", &val); /* not checking return value */
    		ch = getc(fp);           /* ch should be int */
    		new=val;                 //pass numbers to integer form.cos new is defined intefer
    		fprintf(fw,"%d",new); /* don't you need space between numbers? */
    		printf("%lf",val);    //print out the real number.
    		printf("%d",new);      //print out the integer number.
    		
    		if ( ch=='\n' || ch==EOF )/* ch should be int to correctly compare with EOF */
    			printf("\n");  // switch to next line.
    	}while ( ch != EOF );
    	fclose(fp); /* what about closing the over file as well ? */
    	return 0;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #22
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by vart View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
    	FILE *fp,*fw;// declaring file pointers.
    	double val;
    	char ch;
    	int new;        // new for integer part.
    	if((fp = fopen("myfile.txt","r"))==NULL)
    	{
    	printf("sorry,cannot open the file\n");
    	exit(0);
    	}
        fw = fopen("mynewfile.txt","w"); /* not checking return value */
    	do{
    		fscanf(fp, "%lf", &val); /* not checking return value */
    		ch = getc(fp);           /* ch should be int */
    		new=val;                 //pass numbers to integer form.cos new is defined intefer
    		fprintf(fw,"%d",new); /* don't you need space between numbers? */
    		printf("%lf",val);    //print out the real number.
    		printf("%d",new);      //print out the integer number.
    		
    		if ( ch=='\n' || ch==EOF )/* ch should be int to correctly compare with EOF */
    			printf("\n");  // switch to next line.
    	}while ( ch != EOF );
    	fclose(fp); /* what about closing the over file as well ? */
    	return 0;
    }
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
    	FILE *fp,*fw;// declaring file pointers.
    	double val;
    	int ch;
    	int new;        // new for integer part.
    	int i=0;
    	if((fp = fopen("a.txt","r"))==NULL)
    	{
    	printf("sorry,cannot open the file\n");
    	exit(0);
    	}
        if((fw = fopen("mynewfile.txt","w"))==NULL)
    	do{
    		fscanf(fp, "%lf", &val); //read real number from the file. i dont know how the check the return number
    		ch = getc(fp);           //get the following number after the realnumber.
    		new=val;                 //pass numbers to integer form.cos new is defined intefer
    		fprintf(fw,"%d",new);// the numbers are arranged in conclum 3*5, with a space to separate them. 
    		printf("%lf",val);    //print out the real number.
    		printf("%d",new);      //print out the integer number.
    		i++;
    		
    		if ( i%5==0 || ch==EOF )//determin to end the progress if there is a '\n' char or End Of File.
    			fprintf(fw,"\n");  // switch to next line.
    	}while ( ch != EOF );
    	fclose(fp);// what the matter?i just want to close the file
    	return 0;
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Escape Codes
    By renurv in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2006, 01:01 PM
  2. codes
    By IfYouSaySo in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 11-18-2005, 02:09 PM
  3. action replay codes
    By c++.prog.newbie in forum Game Programming
    Replies: 2
    Last Post: 02-28-2004, 07:47 AM
  4. http status codes
    By iain in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-30-2001, 10:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21