-2

Been a little bit rusty in coding but I figured I'd ask this question. I'm trying to create a program that can take a command line filename and scan said file in order to find coordinates of triangles in xlib. I know that xlib takes triangles in the format T(28,100) (23,35) (99,54) from a file a friend gave me, but after looking and trying out different expression in scanf, I can't seem to grab the coordinates in a way that I can use for an algorithm:

 int main(int argc,char *argv[])
{
    // First need to scan the file.
    char *pFilename = argv[1];
    FILE * pFile;
    pFile = fopen(pFilename,"r");
    // char Trig = scanf( ,);
}
Marorin Q
  • 27
  • 7
  • What have you tried yourself? Do you have example data? Also: you probably want to use `fscanf` when operating on the file, not `scanf` (which reads from `stdin`) – Mark Jansen Sep 10 '15 at 12:44
  • I've tried stuff like scanf("T%s9") as an example: Example data would be like: T(20,100( (55,34) (40, 60) I'm only specifically looking for triangles. – Marorin Q Sep 10 '15 at 12:59
  • That already deviates from the format you mentioned in your question. – Mark Jansen Sep 10 '15 at 13:01
  • Well no, I just placed num1-6 there as an example. I really meant it in the way I have it in the comment I placed. Let me edit that. – Marorin Q Sep 10 '15 at 13:11
  • What makes you believe that [Xlib](https://en.wikipedia.org/wiki/Xlib) is parsing files with the `T(28,100) (23,35) (99,54)` syntax? AFAIK, it is completely false! Or please edit your question to give more references. – Basile Starynkevitch Sep 10 '15 at 13:14
  • BTW, if you want to code a GUI application in C++ on Linux, better use [Qt](http://qt.io/) than the raw, very low level, Xlib. – Basile Starynkevitch Sep 10 '15 at 13:15
  • Isn't it possible to use a combination of scanf and perhaps something like bitwise operators in python? That's what I figured how one could do it. – Marorin Q Sep 10 '15 at 13:22

1 Answers1

0

You need to define completely the input syntax (e.g. in EBNF form) of your program and use standard parsing techniques, probably to build some kind of AST in memory (which you would display later on demand). You probably want to read the input line by line, e.g. with std::getline because you might want some readahead.

You need to use standard lexing and parsing techniques (see also this) and probably build some kind of AST in memory; perhaps using code generators like flex (for lexing) and bison or ANTLR (for parsing) should be worthwhile. Otherwise consider some hand written recursive descent parser.

Xlib is an extremely low level but complex library (and you'll need to follow explicitly complex conventions like EWMH). In C++ on Linux, better use Qt or maybe libsdl, or SFML, etc....

Notice that scanf is a C function, not a C++ one.

rici
  • 234,347
  • 28
  • 237
  • 341
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547