c++ file string from text file into 1d array
i need help in C++ arrays
i have a text file with David (name) 1 ( some variable ) bowler 2 (stats) 3 4 i can read the file in and store them into a 1d array however it stores each character in one element when i want to have each line in one element. please can any one help asap thanks code already: #include <stdlib.h> #include <iostream> using namespace std; #include<string> #include<string.h> #include<fstream> #include<iomanip> using std::setw; #include <conio.h> //For getch function using std::cout; using std::endl; const int N = 10; ofstream FOut; //File Out ifstream FIn; //File In string File; //File Name void main() { / char arr[N], *p; cout <<"Enter file name:"<<endl; cin >> File; FIn.open( File.c_str() ); for(int i = 0; i<N; i++) { FIn >> arr[i]; } cout << *(arr) << ""; } |
Store each line in a C++ string. The problem with character arrays is that you need to know their size at compile time.
graeme. |
Code:
#include <cstdlib> |
Thanks for your reply Hivemind and graeme.
the code runs fine Hivemind, however i need it to be stored in a 1d array as i will need access to the elements, throughout the program. i need to make comparsions with the elements to other arrays i have |
The vector I used is one-dimensional..a two dimensional vector would be a vector of vectors of some tyoe.
|
oh ok i see, sorry stupid question i am quite new to c++. to print out one line of the txt file would i have a counter then increment it with line? or how could the vector just point to a particular line in the text file
|
#include <cstdlib>
#include <fstream> #include <iostream> #include <string> #include <vector> int main() { std::cout << "Enter filename: "; std::string filename; cin >> filename; std::ifstream file(filename.c_str()); if(!file) { std::cerr << "Error opening file " << filename << std::endl; return EXIT_FAILURE; } std::string line; std::vector<std::string> contents_of_file; while(getline(file, line)) { contents_of_file.push_back(line); std::cout << "Read line " << line << std::endl; } std::cout << "Read " << contents_of_file.size() << " lines from the file." << std::endl; std::cout << "Any particular line is accessed using contents_of_file[line_number-1]." << std::endl; } |
so if i wanted the line two to be printed i would have this code line:
std::cout << contents_of_file[line_number-1]; line_number has to be declared first to which value |
If you read ten lines into the vector they have indexes 0-9. So if you want the first line, the index should be 0, the last the index would be nine, the fifth the index would 4. line number - 1...
|
i have stored a vector in a class, how do i get access to it from another class?
|
It depends if you made the vector public, private or protected.
If it is public then you can access directly via an object of that class Code:
myObj.theVector[0]; // as an object Code:
myObj.getVector(0); I hope that helps a little. graeme. |
thanks for the tip,
I have a vector vector<string> the; set up in a class as public then in another class what would i do to get access to it. would i have to use a pointer in the first class and then get reference to it? public: myObj.the; myObjPtr->the[0]; thanks graeme |
Just write a simple access method.
Code:
/* class holding a vector with access method, only important bits shown */ value in const string reference if you dont need to modify it to speed up performance. Code:
MyClass myinst; |
i got two classes, 1 class has a vector where values are read in, i want a 2nd class to access the vector from class 1 to check the values in the a particlar element. the main cpp file will call these functions.
how would i go about this? i can't seem to access the vector in another class, and when i do it doesn't display anything. even though i have something in the vector any ideas? |
I just showed you...
|
sorry about that thanks
|
Do it in C
#include <stdio.h>
#include <string.h> #include <malloc.h> #define MAX_STRING_LENGTH 4096 #define MAX_LIST_SIZE 256 int main(int argc, char *argv[]) { FILE *fidFile = NULL; char strBuffer [MAX_STRING_LENGTH] = ""; char *strList[MAX_LIST_SIZE]; int intLast = 0; int intCount = 0; printf ("Test program with %s\n", argv[1]); if ((fidFile = fopen (argv[1], "rt"))) { while (!feof (fidFile) && fgets (strBuffer, MAX_STRING_LENGTH, fidFile)) { printf (strBuffer); strList[intLast++] = (char *)malloc (strlen (strBuffer)); } } for (; intCount < intLast; intCount++) { printf ("%s", strList[intCount]); } return 0; } |
All times are GMT -5. The time now is 05:50 AM. |