LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2006, 04:53 PM   #1
ak3
LQ Newbie
 
Registered: Jan 2006
Posts: 8

Rep: Reputation: 0
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) << "";


}
 
Old 01-17-2006, 04:56 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Store each line in a C++ string. The problem with character arrays is that you need to know their size at compile time.

graeme.
 
Old 01-17-2006, 06:16 PM   #3
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Code:
#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;
   }
}
 
Old 01-17-2006, 07:18 PM   #4
ak3
LQ Newbie
 
Registered: Jan 2006
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 01-17-2006, 07:31 PM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
The vector I used is one-dimensional..a two dimensional vector would be a vector of vectors of some tyoe.
 
Old 01-17-2006, 10:04 PM   #6
ak3
LQ Newbie
 
Registered: Jan 2006
Posts: 8

Original Poster
Rep: Reputation: 0
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

Last edited by ak3; 01-17-2006 at 10:13 PM.
 
Old 01-18-2006, 02:36 AM   #7
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
#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;

}
 
Old 01-18-2006, 07:27 AM   #8
ak3
LQ Newbie
 
Registered: Jan 2006
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 01-18-2006, 07:36 AM   #9
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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...
 
Old 01-19-2006, 11:34 AM   #10
ak3
LQ Newbie
 
Registered: Jan 2006
Posts: 8

Original Poster
Rep: Reputation: 0
i have stored a vector in a class, how do i get access to it from another class?
 
Old 01-19-2006, 02:22 PM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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
myObjPtr->theVector[0]; // as a pointer to the object
If the vector is either private or protected then it is best to write an access method
Code:
myObj.getVector(0);
and the access method will return the appropriate element of the vector (maybe checking that the element actually exists)

I hope that helps a little.

graeme.
 
Old 01-19-2006, 10:14 PM   #12
ak3
LQ Newbie
 
Registered: Jan 2006
Posts: 8

Original Poster
Rep: Reputation: 0
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

Last edited by ak3; 01-20-2006 at 04:29 AM.
 
Old 01-20-2006, 04:22 AM   #13
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Just write a simple access method.

Code:
/* class holding a vector with access method, only important bits shown */
class MyClass
public:
   std::string get_element_in_vector(unsigned short index)
   {
      if(index > the_vector.size())
         throw std::runtime_error("Index out of bounds");

      return the_vector[index]; /* Only reached for valid indexes. */
   }
private:
   std::vector<std::string> the_vector;
};
Call it like you call any non-static member function. You can catch the return
value in const string reference if you dont need to modify it to speed up performance.
Code:
MyClass myinst;

/* Do stuff with myinst, like populating the vector. */

/* I dont need to modify str, so I catch it in a const reference */
const std::string& str = myinst.get_element_in_vector(some_valid_index);

/* str2 to I need to modify, so: */
std::string str2 = myinst.get_element_in_vector(another_valid_index);

Last edited by Hivemind; 01-20-2006 at 04:23 AM.
 
Old 01-20-2006, 06:41 AM   #14
ak3
LQ Newbie
 
Registered: Jan 2006
Posts: 8

Original Poster
Rep: Reputation: 0
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?
 
Old 01-20-2006, 07:04 AM   #15
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
I just showed you...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace a string in a text file jpan Linux - General 3 10-14-2012 06:17 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM
How to pick a string from a text file? sdandeker Linux - General 1 02-12-2004 04:17 AM
Parsing a file for a string of text jamesmwlv Linux - General 2 12-02-2002 07:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration