Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9195789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:42:22+00:00 2026-06-17T21:42:22+00:00

I’m sorry, it would be extremely difficult to make a fully reproducible version of

  • 0

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 data from URL, saves to file 'dataFileName', input control flag 'foreCon'
//                     stu is some object that has string which i want
bool saveData(Manager *man, Stuff *stu, string dataFileName, const int foreCon) {
    char logStr[CHARLIMIT_LARGE];          // CHARLIMIT_LARGE = 2048
    sprintf(logStr, "Saving Data...\n");
    man->logEntry( string(logStr) );       // This appears fine in 'theLog' correctly

    string data = stu->getDataPrefixStr() + getDataFromURL() + "\n";        // fills 'data' with stuff
    data += stu->getDataSuffixStr();

    if( fileExists(dataFileName) ) {
        sprintf(logStr, "save file '%s' already exists.", dataFileName.c_str() );
        man->logEntry( string(logStr) );
        if( foreCon == -1 ) {
            sprintf(logStr, "foreCon = %d, ... exiting.", foreCon);        // LINE 'A' : THIS LINE ENDS UP IN OUTPUT FILE
            tCase->logEntry( string(logStr) );
            return false;
        } else {
            sprintf(logStr, "foreCon = %d, overwriting file.", foreCon);   // LINE 'B' : THIS LINE ENDS UP IN LOG FILE
            tCase->logEntry( string(logStr) );
        }                                                                                                 
    }

    // Initialize output file
    FILE *outFile;
    if( !initFile(outFile, dataFileName) ) {
        sprintf(logStr, "couldn't initFile '%s'", dataFileName.c_str());
        tCase->logEntry( string(logStr) );
        return false;
    }

    fprintf(outFile, "%s", data.c_str());                 // print data to output file

    if( fclose(outFile) != EOF) {
        sprintf(logStr, "saved to '%s'", dataFileName.c_str());
        tCase->logEntry( string(logStr) );
        return true;
    }

    return false;
}

If the file already exists, AND ‘int foreCon = -1′ then the code should print out line ‘A’ to the logFile. If the file exists and foreCon != -1, the old file is overwritten with data. If the file doesn’t exist, it is created, and the data is written to it.

The result however, is that a broken up version of line ‘A’ appears in the data file AND line ‘B’ is printed in the log file!!!!

What the data file looks like:

.. exiting.20130127 161456
20130127 000000,55,17,11,0.00
20130127 010000,54,17,11,0.00
... ...

The second line and onward look correct, but there is an extra line that contains part of line ‘A’.

Now, the REALLY WEIRD PART. If I comment out everything in the if( foreCon == -1) { ... } block, then the data file looks like:

%d, ... exiting.20130127 161456
20130127 000000,55,17,11,0.00
20130127 010000,54,17,11,0.00
... ...

There is still an extra line, but it is the LITERAL CODE copied into the data file.

I think there is a poltergeist in my code. I don’t understand how any of this could happen.


Edit: I’ve tried printing to console the data string, and it gives the same messed up values: i.e. %d, ... exiting.20130127 161456 – so it must be something about the string instead of the FILE *

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:42:23+00:00Added an answer on June 17, 2026 at 9:42 pm

    Answer based on your latest comment:

    getDataPrefixStr() ends up returning a string which starts with
    something like string retStr = COMCHAR + ” file created on …”; such
    that const char COMCHAR = ‘#’;. Could the COMCHAR be the problem??

    You can’t add characters and string literals (which are arrays of char, not strings) like that.

    You’re adding 35 (the ASCII for “#”) to the address of ” file created on … “, i.e. getDataPrefixStr() is whatever starts 35 characters from the start of that string. Since all literal strings are stored together in the same data area, you’ll get strings from the program in the output.

    Instead, you cold do

       const string COMCHAR = "*"; 
       string retStr = COMCHAR + " file created on ...";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.