Could anyone check my noobie C code then give me a kicking XD

ive spent a few days trying to get pointers straight in my head, i think i was ruined by first language being VB.

So after making tons of notes and looking at you tube videos i think i might be getting there, so i'v written a broken binary file copy program.


#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

#define INPUT_FILE "./1.jpg"
#define OUTPUT_FILE "./2.jpg"


void main()
{
	FILE *if_p;		//Input file pointer
	FILE *of_p;		//Output file pointer

	struct stat st;		//Used for file size info
	char *buffer_p;		//using char for 8bit byte, not actualy for chars

	if_p = fopen( INPUT_FILE , "rb");
	if( if_p )
	{
		stat( INPUT_FILE, &st);	//get stat infomation for imput file

		buffer_p = malloc( st.st_size );	//Allocate a buffer to store file in
		fread( buffer_p, 1, st.st_size, if_p);	//read file into buffer

		of_p = fopen( OUTPUT_FILE , "wb");
 		if( of_p )
		{
			fwrite( buffer_p, 1, st.st_size, of_p );	//Write it to ouput file
			fclose( of_p );
		};
		free( buffer_p );	//allways free your objects
		fclose( if_p );
	};
}

So can anyone have a quick look and see if ive effed everytihng up or not?

I've never used "sys/stat.h" so I'm not sure about that.... but otherwise it looks pretty sound. I don't see anything obvious.

You don't actually need the semicolons at the end of each if branch.

Made some adjustments:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

#define INPUT_FILE "./1.jpg"
#define OUTPUT_FILE "./2.jpg"


int main(void)
{
	FILE *if_p;		/* Input file pointer */
	FILE *of_p;		/* Output file pointer */

	struct stat st;		/* Used for file size info */
	char *buffer_p;		/* using char for 8bit byte, not actualy for chars */

	if( (if_p = fopen( INPUT_FILE , "rb")) )
	{
		stat( INPUT_FILE, &st);	/* get stat infomation for input file */

		buffer_p = malloc( st.st_size );	/* Allocate a buffer to store file in */
		fread( buffer_p, 1, st.st_size, if_p);	/* read file into buffer */

 		if( (of_p = fopen( OUTPUT_FILE , "wb")) )
		{
			fwrite( buffer_p, 1, st.st_size, of_p );	/* Write it to ouput file */
			fclose( of_p );
		}
		free( buffer_p );	/* always free your objects */
		fclose( if_p );
	}
        return EXIT_SUCCESS;
}
1 Like

Cheers, Im always worried with C that im going to get something wrong and it all blows up.

You write too many comments :P Write code that's self-explanatory, and use comments if you must (like document why you do X instead of Y). You can easily remove all your comments, and it should be clear to anyone who's somewhat familiar with C to understand what your code does.

1 Like

while im not as familiar with C# as i am with C++ its basically the same syntax/logic

make sure your evaluations never end with a semi colon(dont close it):

if ( input == something); // ERROR
//----------------------------------------------------------------------
if ( input == something)
cout >> "This statement works!";

// you are basically closing the if statement do this instead make sure your statement or command is closed

the same goes with your open and closing brackets:

if (input == something);
{
cout >> "the closing bracket doesnt require a semi-colon";
}; // <----------- THIS RESULTS IN AN ERROR/BUG)

i suggest you pick up an EBOOK and learn to code vs watching video's you will learn more and useful information vs depending on a you tube star. it helps for assignments if you are in a pinch but books are better.

The code looks good. I know you might be starting out in C, but you should start learning C++ now before you get too comfortable. C++ is a much better C and with properly written C++ you should never have to deal with pointers. ;)

1 Like

its good for a beginner to do this. the main problem with developers esp front-end is that they learn through youtube and etc dont correctly document their code. this is very very very annoying in the business world

the best thing you can do for an actual project to help engineers is to pre-document your code an discuss what your goal is with the application. while you dont have to comment on what the declarations are, i would like to know what each function does and what you are trying to accomplish.

if you are a bit while with naming your variables i would always suggest commenting.

I have "The C Programming Language (2nd Edition)" what other books would you recommend? Im not a the professional programmer but learning to help the foss world and fun.

  • A Book on C: Programming in C (4th Edition), by Al Kelley and Ira Pohl. (Entry level)

  • The Standard C Library, by P. J. Plauger

  • Algorithms in C, by Robert Sedgewick

  • C Interfaces and Implementations : Techniques for Creating Reusable Software, by Hanson

  • Expert C Programming: Deep C Secrets, by van der Linden

  • Mastering Algorithms with C, by Loudon

  • C Traps and Pitfalls, by Koenig

  • The Practice of Programming, by Kernighan and Pike

  • Introduction to Algorithms, by Cormen, Leiserson, Rivest, Stein

More books available here: http://iso-9899.info/wiki/Books

Having the standard library and the C standard at hand, helps.

if you have multiple screens i would suggest E-book for coding too. if you haven't used them already. if you have multiple screens this can be much simpler than reading code from a book. and cheaper.

in general i just suggest any college level book. it works

here is a free source if you have morals: https://openlibrary.org/works/OL5042611W/C