473,385 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with Reading files

Hi,

I am storing the name of the text file in a string as
string fname = "abc.txt";
string l_mline;
vector<string> p_vFileIndices;

Then I am trying to read this text file into a vector as follows:

ifstream l_ifsFile(fname.c_str());

while (getline(l_ifsFile, l_mline, '\n'))
{
p_vFileIndices.push_back (l_mline);
}
cout << "Read " << p_vFileIndices.size() << " lines.\n";
for(int i =0; i < p_vFileIndices.size(); i++)
cout<<p_vFileIndices[i]<<endl;

But I see that the file is not read. It says read 0 lines.
Whats the problem in the code.
I am using VC++.

How to convert the read string in vectors to integers??

Please Help.
Thanks!!
Jul 4 '07 #1
4 1436
Hi,

I am storing the name of the text file in a string as
string fname = "abc.txt";
string l_mline;
vector<string> p_vFileIndices;

Then I am trying to read this text file into a vector as follows:

ifstream l_ifsFile(fname.c_str());

while (getline(l_ifsFile, l_mline, '\n'))
{
p_vFileIndices.push_back (l_mline);
}
cout << "Read " << p_vFileIndices.size() << " lines.\n";
for(int i =0; i < p_vFileIndices.size(); i++)
cout<<p_vFileIndices[i]<<endl;

But I see that the file is not read. It says read 0 lines.
Whats the problem in the code.
I am using VC++.

How to convert the read string in vectors to integers??

Please Help.
Thanks!!
Hi flavourofbru,
You find your solution
here

Kiran Parmar
Jul 4 '07 #2
Hi Kiran,

Thanks for the quick reply.
But I will be passing my text file "abc.txt" with different names.
What I am doing is, i am getting certain strings from a text file and then append that string to another one. so each time, I will be reading different text files, thats the reason why I am passing a string having the name of the text file as the file name to read. In that loop I am reading about 100 text files, so each will be of different text file name. So I am passing the name of the text file as string and want to see whether it reads the text files.

Is it possible to send a string as the name of the text file and read it. If so, can you please help me out.

Thanks,
Rishi
Jul 4 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
This code works fine with Visual Studio.NET 2005
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. #include <string>
  5. #include <fstream>
  6. int main()
  7. {
  8. string fname = "c:\\scratch\\instructor\\abc.txt";
  9. string l_mline;
  10. vector<string> p_vFileIndices;
  11.  
  12. //Then I am trying to read this text file into a vector as follows:
  13.  
  14. ifstream l_ifsFile(fname.c_str());
  15.  
  16. while (getline(l_ifsFile, l_mline, '\n'))
  17. {
  18. p_vFileIndices.push_back (l_mline);
  19. }
  20. cout << "Read " << p_vFileIndices.size() << " lines.\n";
  21. for(unsigned int i =0; i < p_vFileIndices.size(); i++)
  22. cout<<p_vFileIndices[i]<<endl;
  23. } //end of main()
  24.  
My abc.txt was:

12 23 45 65
89 34 9 43
95 32 65 39

To convert these to integers, use a stringstream
Expand|Select|Wrap|Line Numbers
  1. stringstream ss;
  2. int inta, intb, intc, intd;
  3. ss << p_vFileIndices[i];
  4. ss >> inta >> intb >> intc >> intd;
  5.  
Jul 4 '07 #4
Thanks!!!
It has been of great help!!
Jul 4 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Birt | last post by:
1. In a program, there will be several files needed to be accessed. Is it necessary to design a class that handles reading/writing files? 2. For a struct data type, struct mystruct { int...
2
by: WAYNEL | last post by:
Hi I am trying to re-write some of the example code that Agilent gives for VB to VB.Net. In .Net I keep getting the error 'cannot change the number of dimensions of an array'. I have paste...
2
by: Parker.Jim | last post by:
I need to write a program which performs word subsitutions on a text file. The program should input the names of three text files: the source file that will be "edited", a text file that contains...
1
by: Michael Palmer | last post by:
I'm reading xml from SQL Server 2K with VB.net using an XSD schema file and SQLXML 3.0. I have the below code working fine, but I'd like to change the code from reading the schema file from a...
6
by: ArunPrakash | last post by:
Hi, I have a web application that looks for a particular string in a set of huge files( the files grow into MBs - max i have seen is 30 MB ). ( search using reg expressions ). the string can occur...
0
by: dgraper | last post by:
Friends: I'm doing some work for a small government client centralizing data from a bunch of little databases into SQL Server. Everything's been easy except for reading in data from a set of...
2
by: nnimod | last post by:
Hi. I'm having trouble reading some unicode files. Basically, I have to parse certain files. Some of those files are being input in Japanese, Chinese etc. The easiest way, I figured, to distinguish...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
6
by: WhiteWizard | last post by:
Hello all, I have been tasked with coming up with a system for providing help for our users, and we have some somewhat unique requirements. NO INTERNET. This is a windows application that will...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.