-2

I'm writing a simulation program in C++. I have several years programming experience but haven't done any at all in 15 years. It is my first time programming in C++. The simulation contains several hundred lines of code and works ok.

My primary goal is the simulation results. Im retired now, so acquiring c++ expertise isn't my primary goal though is still of interest.

I want to format the output results of the simulation into: console output I can read

CSV output I can put through Excel or equivalent

My plan is one routine that can print both formats.

From long examination of stringstream on stack overflow and youtube Im still stuck at square one. l I have two problems :

1: stringstream.str() wont compile

2: The call to fprintf doesn't want to recognise a string

I'm using netbeans 8.1 (standard compiler), zorin linux. Its my first experience of developing on both those platforms. I could write the code to do this myself, but I cant be the first person to want to do this stuff. fwiw the sample code was copied of answers and "howto" on the internet

here are the relevant snippets of code:

#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include "globals.h"
#include "bjClass.h"

using namespace std;

extern FILE * resultsFile;

void print_disk_file(int outstream ) 
{
  std::ostringstream s1;
  int i = 22;
  s1 << "Hello " << i << endl;
  string s2 = s1.str();         // compiler says "unable to resolve str"
  cout << s2;

  // ...

  ostringstream os;
  os << "dec: " << 15 << " hex: " << std::hex << 15 << endl;
  cout << os.str() << endl;

  fprintf(resultsFile, "%s %c" , s1.str(), ' ') ;  // compiler says "unable to resolve str" and gives warning about strings and char *
  fprintf(resultsFile, "%s %c" , s2, ' ') ;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 2
    Please make a [mcve]. – Yunnosch May 25 '19 at 11:11
  • 1
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And as already mentioned, please learn how to create a [mcve] to show us. – Some programmer dude May 25 '19 at 11:12
  • 1
    And you can't use C++ object together with C functions (the `printf` family of functions are C functions). I suggest you [get a couple of good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and learn how to use C++ file streams (like `std::ofstream`). – Some programmer dude May 25 '19 at 11:13
  • 1
    @Someprogrammerdude I can't say you can't, you need extra fluff. Tbh, both C variadic ones and C++ stream "<<" nightmare are both bad in certain ways. Former are easy to mess up in many way before Tuesday, latter are nearly impossible to localize in automated way, ineffective and really were designed with only ANSI and short amount of output in mind. A lot of frameworks offer some variant of both at same time. There are cases where formatted output is preferred. – Swift - Friday Pie May 25 '19 at 11:47
  • By the way, you have two different problems that should be asked about in two different questions. One of them (why you can't use `std::string` as `printf` arguments) would very likely be closed as a duplicate quickly. The first problem (about `s1.str()`) we need a *proper* and *real* [mcve] to be able to help you with, not the example that have been puzzled together by other peoples edits. – Some programmer dude May 25 '19 at 11:53
  • I found the answer to that one. That's NetBeans thing , not based on code, it's settings (or misconfigured toolchain). – Swift - Friday Pie May 25 '19 at 11:56

1 Answers1

1

You have to use data() or c_str() methods of basic_string to get pointer to underlying data.

https://en.cppreference.com/w/cpp/string/basic_string

Namely:

fprintf(resultsFile, "%s %c" , s1.str().c_str(), ' ') ;  
fprintf(resultsFile, "%s %c" , s2.c_str(), ' ') ;

It's not safe to manipulate std::string data directly, of course. c_str() returns pointer to constant buffer which can be used for output routines.

The error message you give is strange.

"unable to resolve str" is not a message from copiler. THat's from NetBeans IDE. here's possible solution netbeans unable to resolve identifier c_str.

PS. In C++ proper header file name is <cstdio>. It's implementation-defined, if it is ok to use stdio.h instead.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42