Avatar billede morphman Nybegynder
18. maj 2005 - 22:32 Der er 2 kommentarer og
1 løsning

C 2 C(plusplus) (sscanf til getline)

Hey, Well, I've made a program coded partly in C and C++. However, I wish that I only use C++

here is the program I've made
http://a.domaindlx.com/morphmanaf/afl2.cpp

What I need to do is insted of usin' sscanf I want to use getline to read the data in But  I just don't know how to convert the sscanf statement to a getline and Remember that the files being read are a comma separated files.

sscanf(buffer, "%[^,],%[^/r]", postnr, by);
sscanf(buffer, "%[^,],%[^,],%[^,],%[^/r]", navn, gade, postnr, by);
sprintf(buffer, "%s,%s,%s,%s", navn, gade, rPostnr.c_str(), rBy.c_str());

Basically, What I want is to convert those lines above usin' getline statement or somethin' else...which I can't think of.

I hope ye can help me out...thank ye very much once again :)
Avatar billede arne_v Ekspert
18. maj 2005 - 22:37 #1
scanf and printf is some of the C features I really like using in C++ but ...

getline has a third argument called delimiter - if you specify comma there
I think the rest would be easy
Avatar billede morphman Nybegynder
19. maj 2005 - 04:06 #2
Could ye provide me with an exsample?
Avatar billede arne_v Ekspert
19. maj 2005 - 10:30 #3
a,bb,ccc
ddd,ee,f

og

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  ifstream f("data.dat");
  while(!f.eof())
  {
      string kol1,kol2,kol3;
      getline(f,kol1,',');
      getline(f,kol2,',');
      getline(f,kol3,'\n');
      cout << kol1 << " " << kol2 << " " << kol3 << endl;
  }
  f.close();
  return 0;
}

skulle illustrere teknikken
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester