473,387 Members | 1,342 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,387 software developers and data experts.

How to read each line of a file to array?

I am trying to read a text file and store each line as a seperate entry in an array. I have tried searching for how to do this but I can only find how to store the whole file into a 1D array.
Oct 21 '10 #1
2 4269
JavierL
17
You should use something like this (c++)

Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main(){
  9.  
  10. ifstream ifs;
  11. ifs.open("filewhatever.txt");
  12. string dat;
  13. vector<string> vecstring;
  14.     while(getline(ifs, dat))
  15.           vecstring.push_back(dat);
  16.  
  17. return 0;
  18. }
  19.  
Oct 22 '10 #2
C code goes like this:


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main(int argc,char *argv[]){
  4.  
  5.    int i=0;
  6.    arr[20][50];//20 is no of line, 50 is length of line
  7.    FILE *fp;
  8.  
  9.       fp=fopen(argv[1],"r");
  10.       if(fp==NULL)
  11.     {
  12.       printf("Unable to open file %s",argv[1]);
  13.       exit(1);
  14.     }
  15.  
  16.     while(fgets(&arr[i][0],50,fp)!=NULL)
  17.     i++;
  18.  
  19. return 0;
  20.  
  21. }
Oct 23 '10 #3

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

Similar topics

20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
4
by: Jeff Rodriguez | last post by:
How would one go about reading a file line by line in reverse? For Example: -- FILE -- 1 2 3 4 5 -- FILE --
6
by: bas | last post by:
hey, I am having a lot of trouble trying to get this program to work properly. It is supposed to read integers from a file and place them into the categories for inventory. The problem seems to...
7
by: eriwik | last post by:
Hi, I'm working on a small application which processes PNG-images and need to read parts of them into structures and/or variables and I was wondering what assumptions one can make about how the...
12
by: mitek777 | last post by:
Hi, I have a problem with reading from file. I would like to find some string(f e.g. name) in file, and if it exist show it on screen with second and third line under. I have this in file: ...
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...
7
by: tackleberi | last post by:
hi, im having some trouble reading a file into java and then storing it in an array here the code i have so far: import java.io.FileNotFoundException; import java.io.FileReader; import...
2
by: Srinivas3279 | last post by:
I am new to the C/C++ My Program: int main(int argc, _TCHAR* argv) { //Declarations FILE *fp;
8
by: omidsoltan | last post by:
Hello, I am new to c++ and have a very simple question. Why when reading a char array the whole array is displayed for example char st = " hello " ; cout << st << endl ; will then display...
1
by: edge691 | last post by:
Hello All, I'm just getting into programming, bit of a newbie. I'm having an issue with the streamreader. I had it working but I've changed something and it has stopped. I have it in a Do...
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: 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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...
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
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...

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.