473,466 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to read from a text file?

19 New Member
Hello,

I am supposed to read from a text file a series of numbers and then store them in a struct array.

Here's my code:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     ifstream In;
  4.     string folder, filename, name2, line;
  5.     int i = 0;
  6.  
  7.     cout << endl << "Enter the name of the folder" << endl;
  8.      cout << "Follow the format: c:/your_folder_name/ :";
  9.      cin >> folder;
  10.      cout << "Enter the name of the file: " ;
  11.      cin >> filename;
  12.  
  13.      name2 = folder + filename + ".txt";
  14.      In.open(name2.c_str());
  15.  
  16.  
  17.    do
  18.    { 
  19.  
  20.          In >> array[i].var1 >> array[i].var2 >> array[i].var3;
  21.          i++;
  22.          cout << i << " " << array[i].id;
  23.  
  24.    } while (getline(In, line));
  25.  
  26.  
  27.    In.close();
  28.    system ("PAUSE");   
  29.    return 0;
  30. }
And this is the input file:

1 1230 1420
2 1150 1115
3 1520 1314
4 1345 1234
5 1130 1230
6 1120 0430
7 1720 1810
8 1122 1125
9 1123 1130
10 1125 1132
23 1145 1122


The code is not reading the file at all. Also the variable 'i' being used is not incrementing either. Could anyone please help?
Mar 10 '11 #1
2 2063
Banfa
9,065 Recognized Expert Moderator Expert
What output do you get?
How is array declared?

You output array[i].id and that member is never assigned to.
Mar 10 '11 #2
kino
19 New Member
Sorry, I should have mentioned it earlier, I declared it as a global array. Thanks for replying, my code is working now, I used vectors to declare an array and get the file data instead of the method above.
Mar 11 '11 #3

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

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
14
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most...
8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
7
by: Drew Berkemeyer | last post by:
Hello, I'm using the following code to read a text file in VB.NET. Dim sr As StreamReader = File.OpenText(strFilePath) Dim input As String = sr.ReadLine() While Not input Is Nothing...
4
by: Nina | last post by:
Hi everyone, Do you know why the following code only read certain number of lines of text file, but not the entire file? Dim sr As StreamReader Dim str As String Dim al As ArrayList = New...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
1
by: John | last post by:
I have a process that reads a text file then uploads the data into a database table. The text file has 10 lines at the end of the file that are blank BUT it appears that the enter key or space bar...
3
by: John | last post by:
How can I tell if a line begins with a number instead of a character? I need to read a text file and some of the data begins with a character which is causing me an issue in reading and uploading...
2
by: jatindersingh80 | last post by:
Hi , i am reading a text file with token # but somtimes blank lines comes between the line at blank line the code gives exception of no such element the text files contains data after blank lines...
3
by: jasvinder singh | last post by:
Respected Sir/madam, Can you help in providing code in 'C' for Reading text file with n number of rows and columns and putting the result in arrays.The sample file is as follows: rim_label =...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.