I want to store my file data in array??

List.txt contains
Xyz Abc
Lmn Pqr
Hij Klm

#include <stdio.h>
#include<conio.h>

int main()
{
   FILE * pFile;
   char mystring [100];
    

   pFile = fopen ("c:\\List.txt" , "r");
   if (pFile == NULL) perror ("Error opening file");
   else
   {
          while(!feof(pFile)) 
          { 
              fgets (mystring , 100 , pFile);
              puts (mystring);
             
          }   
    
   }
   
    fclose (pFile);
   getch();
  
}

But i want to store each string in 2d matrix
like mat[0]=Xyz
mat[1]=Abc

How to do it??

Dani AI

Generated

Short note: the goal is to take lines like Xyz Abc and keep each word separately. was right to point toward C++ streams — they are simpler and safer than manual getc/fgets loops. 's nested-loop idea overcomplicates token input, and 's manual calloc/fread approach is error-prone compared with std::vector/std::string.

A clean C++ approach that stores each line as a pair of strings:

#include <fstream>
#include <string>
#include <vector>
#include <utility>

std::ifstream in("C:\\List.txt");
std::vector<std::pair<std::string,std::string>> mat;
std::string a, b;
if (in) {
    while (in >> a >> b) {           // extracts two whitespace-separated tokens
        mat.emplace_back(a, b);
    }
}

If preserving one token that may contain spaces (or to enforce one pair per line), read lines and parse each with std::istringstream:

#include <sstream>
// ...
std::string line;
while (std::getline(in, line)) {
    std::istringstream iss(line);
    if (iss >> a >> b) mat.emplace_back(a, b); // skip malformed lines
}

Troubleshooting: always check the file opened, avoid nonstandard conio.h/getch(), and do not try to detect EOF with ad-hoc character loops (they often drop the first token). Use vector<string> if a flat sequence (mat[0]=first, mat[1]=second, ...) is preferred instead of pairs.

Recommended Answers

All 4 Replies

well you would need two for loops for a 2D array..something like this

for(i=0;i<row;i++)
{
	cin>>array[i][0];
	for(j=0;j<col;j++)
	{	
		cin>>array[0][j];
	}
}

i'd advice you to use fstream for filehandling, it's easier to use and uses the same trusted << and >> ways of the iostream

Here is my file text file data

Tree Bush
Apple Mango
Animal Dog
Animal2 Cat

#include <stdio.h>
#include<conio.h>
#include<string>
#include<iostream>
#include<fstream>
using namespace std; 

int main()
{
   FILE * pFile;
   char mystring [100];
   char mystring2[100];
   char ch;
    
  int i=0,j=0;
                
   pFile = fopen ("c:\\List.txt" , "r");
   if (pFile == NULL) perror ("Error opening file");
   else
   {
      
          while(!feof(pFile)) 
          { 
                     
             while(getc(pFile)!=' ');
             {
              
               fgets (mystring , 20 , pFile);
               printf("\n");
                         
               puts (mystring);
              
                          
             }             
        
          }   
        
    
    }
   
    fclose (pFile);
    getch();
  
}

i get output
Bush
Mango
Dog
Cat

i m not getting 1st string in line ..
how to get this string??
i want each string individually ??how to get this??

try

while(getc(pFile)!='\n');   //but not ' '(space)

But if you want to store the strings in a 2d array:

//allocate space
2Darray=(char **)calloc(100,sizeof(char *));
for(i=0;i<100;i++) 2Darray[i]=(char *)calloc(100,sizeof(char ));
.............................................................................
//reading letter by letter
i=0;j=0;
while(!feof)
{
fread(2Darray[i][j++],sizeof(char),1,pFile);  // in case of error on this line,try using '&' in front of the '2Darray...'
if(2Darray[i][j-1]=='\n') {i+=1;j=0;}
}

Hope it works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.