Thread: c codes need help,thanks

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    42

    c codes need help,thanks

    QUESTION:The following program reads from a text file containing numeric data stored in columns separated by one blank space. Write the code and compile it on your computer. Create a text file called myfile.txt containing a few columns (3×5 for example) of numeric data (real numbers) and test the program.
    #include <stdio.h>
    int main(void)
    {
    FILE *fp;
    double val;
    char ch;

    fp = fopen("myfile.txt","r");
    do{
    fscanf( fp, "%lf", &val );
    ch = getc(fp);
    printf(" %lf",val);
    if ( ch=='\n' || ch==EOF )
    printf("\n");
    } while ( ch != EOF );
    fclose(fp);
    return 0;
    }
    Analyse the code and include comment lines explaining how the program achieves its task. Modify the code so that the program additionally creates a new text file where it stores only the integer part (discard the factionary part) of the real numbers stored in myfile.txt. The new file should write data in the same format (3×5 with columns separated by space) as the original file. Include statements to check if the file was opened and closed successfully. Submit only the final, commented code.


    Code:
    #include <stdio.h>
    int main(void)
    {
    	FILE *fp,*fw;// declaring file pointers.
    	double val;
    	char ch;
    	int new;        
    	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;
    		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;
    }
    i am unsure how to get the integer part of real number.

    10-2.c: In function ‘main’:
    10-2.c:12: warning: incompatible implicit declaration of built-in function ‘exit’

    the message return from compiler.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hth373737 View Post
    i am unsure how to get the integer part of real number.
    Well, getc() is the wrong way!

    If you have a floating point value from a calculation, how would you produce the integer part? I presume that file handling isn't the first part of your exercises in C? Maybe you need to look back into the chapters about data types, floating point in particular.

    10-2.c: In function ‘main’:
    10-2.c:12: warning: incompatible implicit declaration of built-in function ‘exit’

    the message return from compiler.
    You need to include the header file that contains the function exit() - stdlib.h.

    --
    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.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> i am unsure how to get the integer part of real number.

    math.h has a function floor( ) that does just that.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by Sebastiani View Post
    >> i am unsure how to get the integer part of real number.

    math.h has a function floor( ) that does just that.
    oops,that function is beyond my understanding~~

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by matsp View Post
    Well, getc() is the wrong way!

    If you have a floating point value from a calculation, how would you produce the integer part? I presume that file handling isn't the first part of your exercises in C? Maybe you need to look back into the chapters about data types, floating point in particular.



    You need to include the header file that contains the function exit() - stdlib.h.

    --
    Mats
    define a variable in integer form and print it out with the integer part?

    cannot do that?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hth373737 View Post
    define a variable in integer form and print it out with the integer part?

    cannot do that?
    What is it that you can not do? What does your current code look like, and what is the problem with it?

    --
    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.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by matsp View Post
    What is it that you can not do? What does your current code look like, and what is the problem with it?

    --
    Mats
    Code:
    
    #include <stdio.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("%f",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;
    }

    appledemacbook:documents apple$ gcc 10-2.c
    10-2.c: In function ‘main’:
    10-2.c:12: warning: incompatible implicit declaration of built-in function ‘exit’

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you read post #2, you will find the answer to why exit() is undeclared.

    --
    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.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by matsp View Post
    If you read post #2, you will find the answer to why exit() is undeclared.

    --
    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("%f",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;
    }
    1.1 2.2 3.3 4.4 5.5
    1.2 2.3 3.4 4.5 5.6
    1.3 2.4 3.5 4.6 5.7


    ye. the compiler is okay, but it cannot be run.
    it cannot return any result when the command ./a.out

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hth373737 View Post
    ye. the compiler is okay, but it cannot be run.
    it cannot return any result when the command ./a.out
    Huh? Can you rephrase that, as I don't understant what you are asking for...

    --
    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.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by matsp View Post
    Huh? Can you rephrase that, as I don't understant what you are asking for...

    --
    Mats
    the compiler has not said the codes are wrong. but as the question asked i do,
    the 3*5 matrix number are stored in myfile.txt
    it cannot run when ues the program.
    it was okay with the codes in the question.

    i am useing macbook, gcc 4.2

    does it matter?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Look VERY carefully on this line:
    Code:
    	if((fp = fopen("myfile.txt","r"))==NULL);
    Or, add "-Wall -Wextra" (without the quotes) to the gcc command-line, and it will actually tell you. That is generally a good idea anyways, as most fo the warnings you get from that will be more or less valid errors.

    --
    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.

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by matsp View Post
    Look VERY carefully on this line:
    Code:
    	if((fp = fopen("myfile.txt","r"))==NULL);
    Or, add "-Wall -Wextra" (without the quotes) to the gcc command-line, and it will actually tell you. That is generally a good idea anyways, as most fo the warnings you get from that will be more or less valid errors.

    --
    Mats
    appledemacbook:documents apple$ -Wall -Wextra
    -bash: -Wall: command not found


    what is the matter? i am new to unix command.

    i was use VS instead.

    how to verify the codes with Mac OS X command terminal?
    thanx



    hth

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hth373737 View Post
    appledemacbook:documents apple$ -Wall -Wextra
    -bash: -Wall: command not found


    what is the matter? i am new to unix command.

    i was use VS instead.

    how to verify the codes with Mac OS X command terminal?
    thanx



    hth
    Erh, no. You want to use "gcc -Wall -Wextra [optionally other stuff] yourfile.c" to compile the code.

    --
    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.

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by matsp View Post
    Erh, no. You want to use "gcc -Wall -Wextra [optionally other stuff] yourfile.c" to compile the code.

    --
    Mats
    appledemacbook:documents apple$ gcc -Wall -Wextra [myfile.txt] 10-2.c
    i686-apple-darwin9-gcc-4.0.1: [myfile.txt]: No such file or directory
    10-2.c: In function ‘main’:
    10-2.c:10: warning: empty body in an if-statement
    appledemacbook:documents apple$ gcc -Wall -Wextra myfile.txt 10-2.c
    10-2.c: In function ‘main’:
    10-2.c:10: warning: empty body in an if-statement
    ld warning: in myfile.txt, file is not of required architecture
    appledemacbook:documents apple$


    i do have state the if-statement.
    Code:
    if((fp = fopen("myfile.txt","r"))==NULL);
    if the value of fopen is NULL, it will exit...

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, 02:01 PM
  2. codes
    By IfYouSaySo in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 11-18-2005, 03:09 PM
  3. action replay codes
    By c++.prog.newbie in forum Game Programming
    Replies: 2
    Last Post: 02-28-2004, 08: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