Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,155,171 members, 7,825,658 topics. Date: Sunday, 12 May 2024 at 08:05 PM

I Haz Bugz In My Codez :( - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / I Haz Bugz In My Codez :( (914 Views)

(2) (3) (4)

(1) (Reply) (Go Down)

I Haz Bugz In My Codez :( by syntax3rror: 5:39am On Oct 24, 2014
So I started learning C like a week ago, and I for the life of me cannot figure out how to fix this simple script that I am writing. It is supposed to output the contents of a file with line numbers added, but what it does is just prints this:

1: MasterOfTheUniverse:~ syntax3rror$

Here are my codez:

#include <stdio.h>
#include <stdlib.h>

void display_usage(void);

int line;

main(int argc, char *argv[])
{
char buffer[256];

FILE *fp;

if( argc < 2 )
{
display_usage();

exit(1);
}

if (( fp = fopen( argv[1], "r" )) == NULL )
{
fprintf( stderr, "Error opening file, %s!", argv[1] );

exit(1);
}

line = 1;

while( 1 == 1 )
{
fprintf( stdout, "%d:\t%s", line++, buffer );

fclose(fp);

return 0;
}
}

void display_usage(void)
{
fprintf(stderr, "\nProper Usage is: " );

fprintf(stderr, "\n\n/list filename.ext\n" );
}

I am pretty sure that the bug is on line 30, while( 1 == 1 ). Previously that line was while( fgets( buffer, 256, fp ) != NULL ), but then I had a different bug, it just printed the first line and then stopped, I think because it encountered the null char at the end of the line and thought it was a null pointer. Dumb little compiler. I considered just making it so that you would have to put a certain EOF character at the end of the file to tell it to stop, but then that would be cheating since you would have to modify the file. I don't even know why while( 1 == 1) won't work, it should always evaluate to true and then it keeps looping through the file until it reaches the end, right? But that wasn't what it did. I am kind of confused right now?
Re: I Haz Bugz In My Codez :( by bot101(m): 7:16am On Oct 24, 2014
sowi u hear
Re: I Haz Bugz In My Codez :( by Nobody: 7:04am On Oct 25, 2014
I use BAYGON SPRAY to kill all my bugs.

3 Likes

Re: I Haz Bugz In My Codez :( by blueyedgeek(m): 10:49am On Oct 25, 2014
dhtml18:
I use BAYGON SPRAY to kill all my bugs.
grin Very helpful sir.
Re: I Haz Bugz In My Codez :( by VictorAkujobi: 12:32am On Oct 26, 2014
Hey Syntax3rror,

First, well done for learning C. Now on to your bug...

while (1==1) : This is an infinite loop. The only reason it's not working is because of the return statement. The code is run only once and it breaks out of the loop.

So if I were to fix this, I would

1. Put back the fgets statement back in the while loop. This says keep looping until the pointer I receive is a NULL one, hence no string exists anymore. (1)

2. Since you now have your break condition (1), you can remove the return(0).


If you really like to use a low level language like C, consider using https://golang.org/. It's quite low level and has performance similar to Java, is compiled and makes programming web services much easier. I'm currently using it for my startup https://stylestation.co.

Cheers

V

(1) (Reply)

30 Brilliant African Tech Startups / Women Techmakers Port Harcourt Presents Code Day, 2015 / Data Editing Without Graphical Controls

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 14
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.