problem about fprintf-Collection of common programming errors


  • nategoose
    c multithreading gdb pthreads fprintf
    I’m having a very tough time debugging a multi-threaded C application that I’ve made a few changes to. I’ve been unable to use GDB to help identify the issue(see below code for more info).The following code is from one of the tasks that is opened in its own thread. I’ve snipped out most of the code following the problem. void tskProcessTenMinuteTables(void *input) {/* Check the minute as soon as we start. If we’re started on a ten min* boundary, sleep for one minute.*/time_t now;time_t wakeup;struct tm *next_tick_ptr;now = time(NULL);next_tick_ptr = localtime(&now);/* returns a time struct populated w/ next ten min boundary */GetNextTenMinBoundary(next_tick_ptr); wakeup = mktime(next_tick_ptr);sleep(2); /* Without this sleep, the following

  • AusCBloke

  • zhermes
    c++ c file sprintf fprintf
    I’m sorry, it would be extremely difficult to make a fully reproducible version of the error — so please bare with my schematic code.This program retrieves information from a web page, processes it, and saves output to an ASCII file. I also have a ‘log’ file (FILE *theLog—contained within a Manager object) for reporting errors, etc.Some background methods:// Prints string to log file void Manager::logEntry(const string lstr) {if( theLog != NULL ) { fprintf(theLog, “%s”, lstr.c_str()); } }// Checks if file with given name already exists bool fileExists(const string fname) {FILE *temp;if( temp = fopen(fname.c_str(), “r”) ) { fclose(temp);return true;} else { return false; } }// Initialize file for writing (some components omitted)… bool initFile(FILE *&oFile, const string fname) {if(oFile = fopen(fname.c_str(), “w”) ) { return true; }else { return false; } }The stuff causing trouble:// Gets

  • Alex
    c null printf fprintf
    I have a problem I can’t grasp with printf. It’s the first time ever I have this problem, so I’m sure it’s something naive, but no matter what, I can’t solve it myself… maybe it’s just because I’m tired: fprintf (and i’ve found it’s true also for printf) correctly prints only the first argument, from the second it will print only “0” for numbers and “(null)” for stringsHere’s the relevant code:

  • jumar
    fprintf fflush
    To I came upon this line of code:fprintf(stdout, “message”, fflush(stdout));Note that the message does not contain any %-tag.Is that safe

  • Joachim Pileborg
    c c99 language-lawyer fprintf variadic-functions
    In section 7.19.6.1 paragraph 8 of the C99 standard:c If no l length modifier is present, the int argument is converted to anunsigned char, and the resulting character is written.In section 7.19.6.1 paragraph 9 of the C99 standard:If any argument is not the correct

  • AnthonyJK
    c file file-io udp fprintf
    I’m trying to get a simple send and receive UDP program working, but I’m having a bit of trouble with saving the received data. As far as I can tell the data is being sent and received properly, as I’ve printed it out on both ends. Before I write the data to the file (if I just print out the received chunk) it doesn’t have any extra characters, so I’m a bit lost as to where they are coming from.When I append each chunk of received data to the file it adds a “^P^B^GÐ^?” after every chunk written. for example one of the chunks ended with “We, therefore^P^B^GÐ^?,” instead of “We, therefore,”.Any help is appreciated, thanks in advance.UPDATE:I’ve seemed to have gotten things working semi-better, I’m now having an issue with it replacing the first character of ever

  • rolls
    c programming-languages sprintf fprintf snprintf
    Here is my code, basically one the 4 computer I have tested it on they all work perfectly with very large data sizes, eg textfiles up to 500mb in size, but when I run them on the server with real data even files as small as 6mb seem to overrun somewhere and writes garbage to the end of my files.Here is the source of the entire function so people can have a more indepth look/** Reads values from tagname between start_time and end_time which are strings in the format of 01/01/1970-12:00, a null string is treated as 01/01/1970, values are stored in”tagname”.csv */ int ReadValues(char * tagname, char * start_time, char * end_time, char * working_directory, char * new_tag_name) {long lRet;int number_of_samples;int loop;IHU_DATA_SAMPLE * pSamples=NULL;IHU_TIMESTAMP StartTime;IHU_TIMESTAMP EndTime;FILE *stream;char outputFileName[200];char szQuality[100];char newTempTagName[100];int Year;int Month;int Day;int Hour;int Minute;int Second;long Subsecond;int date[10];//if we want to change the tagname do it nowif(new_tag_name != 0){strncpy(newTempTagName, new_tag_name, 100);} else {strncpy(newTempTagName, tagname, 100); } // if the tagname contains a character that is invalid for a filename then we have to make a nameif ( (strstr(newTempTagName, “/”)) || (strstr(newTempTagName, “\\”)) || (strstr(newTempTagName, “:”))){sprintf(outputFileName, “MadeUpTag%d”, MadeUpTagCount++);}else{//If a working directory was passed in use itif(!working_directory){ strncpy(outputFileName, newTempTagName,199); } else {strcpy(outputFileName, working_directory); //Copy the working directory to the filename// Append a / on the end of the working directory if it doesn’t already have oneif(outputFileName[strlen(outputFileName)-1] == ‘\”‘){ outputFileName[strlen(outputFileName)-1] = 0;} if(outputFileName[strlen(outputFileName)-1] != ‘\\’){ strncat(outputFileName, “\\”, 199); }strncat(outputFileName, newTempTagName, 199); //Copy the tagname to the end of the working directory}}//Add the csv file extensionstrcat(outputFileName, “.csv”);#ifdef DEBUGprintf(“Output filename: %s\n”, outputFileName);#endifstream = fopen(outputFileName, “w”);if( stream == NULL ) {printf(“The file %s can not be opened\n”, outputFileName );} else {//If start_time == 0 we want to start at 1970if(start_time == 0){// we want all the data so use an old start timestruct tm local;local.tm_year = date[2] – 1900;local.tm_mon = date[1] – 1;local.tm_mday = date[0];local.tm_hour = date[3];local.tm_min = date[4];local.tm_sec = 0;time_t utc_seconds = mktime(&local);StartTime.Seconds = (long)utc_seconds;StartTime.Subseconds = 0; } else {//we have been supplied a start time#ifdef DEBUGprintf(“Start Time: “);#endiflRet = convert_date(start_time, &StartTime);#ifdef DEBUGprintf(“Seconds %ld \n”, StartTime.Seconds);#endif}//if end_time == 0 we want to go all the way until now.if(end_time == 0){// end time of 0 means nowmemset(&EndTime, 0, sizeof(EndTime));} else {//we have been supplied an end time#ifdef DEBU

Originally posted 2013-11-09 23:15:25.