Hi ,

A simple stupid question: I am to read the contents of a file that goes this way

RXbytes = 0 b TXbytes = 0 b
RXbytes = 3 Kb TXbytes = 2 Kb
RXbytes = 119 Kb TXbytes = 6 Kb
RXbytes = 189 Kb TXbytes = 5 Kb
RXbytes = 189 Kb TXbytes = 5 Kb
RXbytes = 189 Kb TXbytes = 5 Kb
RXbytes = 189 Kb TXbytes = 5 Kb

and gives this error: & Illegal operator for pointer 2
*** Interpreter error recovered ***

Since = is an operator, is it appropriate to specify it as char? Is it the one that causes trouble in fscanf?
Many thanks

F.

TH1F readThroughput(char *fname, int nread) {
  char rec, trans, equal0, equal1;
  float rxThoughput, txThroughput;
  int i;
  char RXunit, TXunit;
  TH1F *h = new TH1F("h1","Throughput", nread, 0.5, nread-0.5);

  FILE *fp = fopen(fname, "r");
  if (fp == NULL) {
	printf("Cound not open file %s\n",fname);
  	return h;
  }

  for (i=0; i<nread; i++)  {
	printf("Can you read me?");
	fscanf(fp,"%c %c %f %c %c %c %f %c" &rec, &equal0&rxThoughput, &RXunit, &trans, &equal1, &txThroughput, &TXunit);
	h->SetBinContent(i+1, rxThoughput);
  }

  fclose(fp);
  h->Sumw2();
  return h;
}

Recommended Answers

All 2 Replies

Hello fclopez,
You have missed some ',' in the line 16. Make sure that you didn't miss them in the program.

fscanf(fp,"%c %c %f %c %c %c %f %c" &rec, &equal0&rxThoughput, &RXunit, &trans, &equal1, &txThroughput, &TXunit);

You are trying to read "RXBytes" into one character (since you are using %c and rec is just a character).

I would read the whole line in using fgets and copy out/parse the portions that you need based on the fact that your have an equals sign, followed by a space, followed by a number (since the text is the same each time you don't really need to keep copying it out). You're not limited to fgets, as you're using C++, so you could possibly look into using a stringstream as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.