![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 6
| Display return value from a function Code: if ( readFile() != 0 )
{
fprintf(stderr, "Unable to read input file!\n");
program_exit(E_FILES_READ);
}
printf("equals zero \n");
int readFile()
{
char revAccName[51];
char custSortCode[7];
char custAccNum[9];
char custAccName[19];
char amount[12];
char date[9];
char id1[21];
char id2[21];
char id3[21];
char reason[2];
char record[292];
cur = &head;
for(;;)
{
if ( !fgets(record, 291, inFile) )
break;
if (strlen(record) < 101)
return 1;
strncpy(revAccName, &record[5], 50);
revAccName[50] = 0;
strncpy(custSortCode, &record[55], 6);
custSortCode[6] = 0;
strncpy(custAccNum, &record[61], 8);
custAccNum[8] = 0;
strncpy(custAccName, &record[69], 18);
custAccName[18] = 0;
strncpy(amount, &record[87], 11);
amount[11] = 0;
strncpy(id1, &record[106], 20);
id1[20] = 0;
strncpy(id2, &record[126], 20);
id2[20] = 0;
strncpy(id3, &record[146], 20);
id3[20] = 0;
strncpy(reason, &record[166], 1);
reason[1] = 0;
/* Allocate space for the record and hook it up to list. */
*cur = (unpaids_t *)calloc(1, sizeof(unpaids_t));
if (!*cur)
return 1;
strcpy((*cur)->revAccName, &revAccName[strspn(revAccName," ")]);
strcpy((*cur)->custSortCode, &custSortCode[strspn(custSortCode," ")]);
strcpy((*cur)->custAccNum, &custAccNum[strspn(custAccNum," ")]);
strcpy((*cur)->custAccName, &custAccName[strspn(custAccName," ")]);
strcpy((*cur)->amount, &amount[strspn(amount," ")]);
strcpy((*cur)->id1, &id1[strspn(id1," ")]);
strcpy((*cur)->id2, &id2[strspn(id2," ")]);
strcpy((*cur)->id3, &id3[strspn(id3," ")]);
strcpy((*cur)->reason, &reason[strspn(reason," ")]);
(*cur)->next = NULL;
cur = &((*cur)->next);
}
}
Since moving to a new unix box it fails. Do we think the version of the OS needs to explicitly have a return 0; It's not my code it's someone elses and I am loathe to change anything because it has always worked before. Sun 5.9 original SunOS 5.10 now. Adding in the return 0 fixes it. But the program does work on 5.9 I would also maybe like to print the return code any suggestions ? |
| mapleleafblue is offline | |
| | #2 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
The fact that it worked somewhere once does not mean it is or ever was correct. It is not proper C code without the return value, so don't feel bad about correcting it no matter who wrote it. The reason it probably worked before is because 0 == untrue in C, so perhaps the other compiler automatically assigned untrue to a function that had no proper return value; in other words, that programmer was relying on *luck* because this is "undefined behavior" (and this programmer obviously did not test it on more than one compiler and possibly ignored warnings on the one that does make it work).
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 06-30-2009 at 05:54 AM. | |
| MK27 is offline | |
| | #3 |
| pwning noobs Join Date: Jun 2009 Location: The Great White North
Posts: 125
| you need a return statement at the bottom of your function. It may seem to work on some systems because you are unlucky, but the return value will be undefined if there is not explicit return statement.
__________________ Sun Certified Java Programmer / Developer IEEE CSDP |
| Zlatko is offline | |
| | #4 | |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Quote:
.Here's something you can try. Compile the source on 5.9 and move the executable to the 5.10 box and run it there to see if that makes a difference. If backwards compatibility is preserved in the compiler versions, this should work and pinpoint the cause. | |
| itCbitC is offline | |
| | #5 |
| Registered User Join Date: Jan 2008 Location: Seattle
Posts: 476
| You do in fact have return values for your function: Code:
...
if (strlen(record) < 101)
return 1;
...
if (!*cur)
return 1;
The problem here is that either one of those will have to be successful in order to satisfy the return value of the function call. From the use of your for(; you better! |
| slingerland3g is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Why only 32x32? (OpenGL) [Please help] | Queatrix | Game Programming | 2 | 01-23-2006 02:39 PM |
| <Gulp> | kryptkat | Windows Programming | 7 | 01-14-2006 01:03 PM |
| C++ FTP class won't work | lord mazdak | C++ Programming | 8 | 12-18-2005 07:57 AM |
| Binary Search Trees Part III | Prelude | A Brief History of Cprogramming.com | 16 | 10-02-2004 03:00 PM |
| Request for comments | Prelude | A Brief History of Cprogramming.com | 15 | 01-02-2004 10:33 AM |