__________________
Senast redigerad av erikp121 2018-09-23 kl. 02:55.
Senast redigerad av erikp121 2018-09-23 kl. 02:55.
#include <iostream> #include <fstream> int main(){ std::ifstream dataFile; dataFile.open("../data/test.txt"); if (dataFile.is_open()){ std::string line{""}; while (std::getline(dataFile,line)){ std::cout << line << "\n"; } dataFile.close(); } else { std::cout << "Unable to open file...\n"; } return 0; }
#include <iostream> #include <fstream> int main(){ std::ifstream dataFile; dataFile.open("../data/test.txt"); if (dataFile.is_open()){ std::string line{""}; while (std::getline(dataFile,line)){ std::cout << line << "\n"; } dataFile.close(); } else { std::cout << "Unable to open file...\n"; } return 0; }
#include <iostream> #include <fstream> int main(int numArgs, char *args[]){ std::string aux(args[0]); int pos = aux.rfind('/'); std::string path = aux.substr(0,pos+1) std::string dataPath{""}; dataPath = path + "../data/test.txt"; std::ifstream dataFile; dataFile.open(dataPath); if (dataFile.is_open()){ std::string line{""}; while (std::getline(dataFile,line)){ std::cout << line << "\n"; } dataFile.close(); } else { std::cout << "Unable to open file...\n"; } return 0; }
#include <iostream> #include <fstream> #ifdef TEST_DATA_PATH std::string dataPath; #endif int main(){ std::ifstream dataFile; dataFile.open(dataPath); if (dataFile.is_open()){ std::string line{""}; while (std::getline(dataFile,line)){ std::cout << line << "\n"; } dataFile.close(); } else { std::cout << "Unable to open file...\n"; } return 0; }
CXX = g++ COMPILER_FLAGS = -Wall -s -DTEST_DATA_PATH=/path/to/test/data/test.txt LINKER_FLAGS = OUTPUTNAME = test SRC = ./src INC = ./include BIN = ./bin OBJ = ./obj MAIN = $(SRC)/main.cpp ALLOBJECTS = $(OBJ)/*.o main : $(MAIN) $(CXX) $(COMPILER_FLAGS) -c $(MAIN) && mv *.o $(OBJ) test : $(OBJ) $(CXX) $(OBJ)/*.o $(LINKER_FLAGS) -o $(BIN)/$(OUTPUTNAME) all : $(CXX) $(SRC)/*.cpp $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(BIN)/$(OUTPUTNAME) clean : rm -rf $(BIN)/$(OUTPUTNAME) $(OBJ)/*.o
/path/to/test total 36 drwxrwxr-x. 8 erik erik 4096 Sep 23 19:23 . drwxrwxr-x. 3 erik erik 4096 Sep 23 18:54 .. drwxrwxr-x. 2 erik erik 4096 Sep 23 19:22 bin drwxrwxr-x. 2 erik erik 4096 Sep 23 18:54 data drwxrwxr-x. 2 erik erik 4096 Sep 23 18:54 include -rw-rw-r--. 1 erik erik 499 Sep 23 19:23 makefile drwxrwxr-x. 2 erik erik 4096 Sep 23 18:54 obj drwxrwxr-x. 2 erik erik 4096 Sep 23 19:24 src drwxrwxr-x. 2 erik erik 4096 Sep 23 18:54 test
#define QUOTE(str) #str #define EXPAND_AND_QUOTE(str) QUOTE(str) dataFile.open(EXPAND_AND_QUOTE(TEST_DATA_PATH));
dataFile.open(TEST_DATA_PATH);
std::string dataPath = TEST_DATA_PATH; /* eller hur man nu initialiserar en C++-string */
#include <iostream> #include <fstream> #include "../include/read_conf_file.h" #include "../include/read_data_file.h" #ifdef HELLO_CONF_PATH std::string defaultConfig = HELLO_CONF_PATH; #endif int main(int argc, char* argv[]){ std::string dataPath; if (argc == 1){ dataPath = readConfFile(defaultConfig); } else if (argc == 2){ dataPath = readConfFile(argv[1]); } else if (argc > 2){ std::cerr << "Unsupported behaviour. Use 0 or 1 argument.\n"; } if (argc <= 2){ readDataFile(dataPath); } return 0; }
#include <iostream> int main(){ std::string userInput; int x{0}; int y{0}; bool myLoop{true}; while (myLoop){ if (userInput != "exit"){ std::cout << "Enter x: "; std::cin >> userInput; if (userInput != "exit"){ x = std::stoi(userInput); std::cout << "Enter y: "; std::cin >> userInput; if (userInput != "exit"){ y = std::stoi(userInput); std::cout << x << " + " << y << " = " << x + y << "\n"; } } } else if (userInput == "exit"){ myLoop = false; } } }
#define TRIPLE(n) n * 3 cout << TRIPLE(myvar + 1);
#define TRIPLE(n) ((n) * 3)
#define SQUARE(n) ((n) * (n)) cout << SQUARE(n++);
for (;;) { /* läs en rad från stdin */ if (/* det blev EOF */) break; /* konvertera till heltal */ /* gör något med heltalet */ }
makestats # in från terminalen, ut till terminalen makestats < baseballscores.txt > baseballstats.txt # in från en fil, ut till en annan printf '90\n34\n54\n6\n' | makestats primes 0 1000 | makestats | mail -s 'Primtalsstatistik' mamma@jobbet.se
#include <iostream> #include "../include/get_value.h" #include "../include/print_result.h" int main(){ std::string userInput; int x{0}; int y{0}; bool myLoop{true}; while (myLoop){ std::cout << "Enter x: "; x = getValue(userInput); if (userInput != "exit"){ std::cout << "Enter y: "; y = getValue(userInput); } if (userInput != "exit"){ printResult(x, y); } if (userInput == "exit"){ myLoop = false; } } return 0; }
#include <iostream> #include "../include/get_value.h" int getValue(std::string &userInput){ int value{0}; std::cin >> userInput; if (userInput != "exit"){ try { value = std::stoi(userInput); } catch(...){ std::cout << "Invalid input! Input = " << value << "\n"; } } return value; }
#include <iostream> #include "../include/print_result.h" #include "../include/my_division.h" void printResult(const int x, const int y){ std::cout << x << " + " << y << " = " << x + y << "\n"; std::cout << x << " - " << y << " = " << x - y << "\n"; std::cout << x << " * " << y << " = " << x * y << "\n"; myDivision(x, y); }
#include <iostream> #include <quadmath.h> #include "../include/my_division.h" void myDivision(const int x, const int y){ if ((y != 0) && (x % y != 0)){ __float128 z{0}; z = (__float128)x / (__float128)y; char buffer[128]; quadmath_snprintf(buffer, sizeof buffer, "%.32Qf", z); std::cout << x << " / " << y << " = " << buffer << "\n"; } else if (y != 0){ std::cout << x << " / " << y << " = " << x / y << "\n"; } else if (y == 0){ std::cout << "Divide by 0 error...\n"; } return; }
Flashback finansieras genom donationer från våra medlemmar och besökare. Det är med hjälp av dig vi kan fortsätta erbjuda en fri samhällsdebatt. Tack för ditt stöd!
Swish: 123 536 99 96 Bankgiro: 211-4106
Flashback finansieras genom donationer från våra medlemmar och besökare. Det är med hjälp av dig vi kan fortsätta erbjuda en fri samhällsdebatt. Tack för ditt stöd!
Swish: 123 536 99 96 Bankgiro: 211-4106