Assigning variables after parsing file with c++ -
i'm looking little guidance or particular barrier i'm having in c++. i'm coming python background of things confusing me. i'm taking text file command line argument , attempting parse/assign variables things i've read in text. i've made super simple text file, , deem super simple cpp file. did write based off of of other advice similar questions saw answered on here.
in python, implement quick regex sort .readlines() function , assign variables, , know won't quite easy in cpp heres i've got:
#include <fstream> #include <iostream> using namespace std; int main(int argc, char *argv[]) { if (argv > 1) { std::ifstream s(argv[1]); if (s.is_open()) ; // compiler complained unless on own line { int i, j, k; // assign ints, no idea why s >> >> j >> k; // std::cout << << endl; std::cout << j << endl; std::cout << k << endl; // repeat same chars, try assign file reads? } } }
and text file has:
5 3 1
i'm expecting see output program "5 \n 3 \n 1"
which isnt happening. i'm looking have target line : "truck 500" , "truck", assigning int truck variable "500"
i'm sorry if question on place, or references right direction welcomed. thanks!
first off, semicolon after if
-statement complete conditional block of if
-statement (and absolutely can go on previous line don't want have semicolon in first place). in addition, always need check input after reading! stream has no idea going attempt next , can't predict whether successful before trying. is, code should this:
std::ifstream in(argv[1]); if (!in) { std::cout << "error: failed open '" << argv[1] << "' reading\n"; } else { int i, j, k; if (std::cin >> >> j >> k) { std::cout << "read i=" << << " j=" << j << " k=" << k << '\n'; } else { std::cout << "error: there format error\n"; } }
that said, based on code should see expected output assuming you, indeed, managed correctly open file. i'd guess code above point out what's going wrong.
Comments
Post a Comment