I nees help to make this code read data from a file a given file. #include <iost
ID: 3630130 • Letter: I
Question
I nees help to make this code read data from a file a given file.
#include <iostream>
#include <fstream>
char** read(const char* fileName, int& count)
{
std::ifstream
countingStream(fileName);
// first count them
count = 0;
while (true) {
char line[100];
countingStream.getline(line, 100);
if (strlen(line)== 0) {
break;
}
count += 1;
}
countingStream.close();
std::ifstream
readingStream(fileName);
char** words = new char* [count];
for (int index = 0;
index < count; ++index) {
char line[100];
readingStream.getline(line, 100);
words[index] = _strdup(line);
}
Explanation / Answer
Dear Friend
the code will read a file now but u didn`t mention what to do with it
PLEASE RATE
#include <iostream>
#include<string>
#include <fstream>
using namespace std;
void read(const char* fileName, int& count)
{
std::ifstream countingStream(fileName);
// first count them
count = 0;
while (true)
{
char line[100];
countingStream.getline(line, 100);
for(int i=0;i<strlen(line);i++)
cout<<line[i]<<" ";
if (strlen(line)== 0)
{
break;
}
count += 1;
}
countingStream.close();
std::ifstream
readingStream(fileName);
char** words = new char* [count];
for (int index = 0;index < count; ++index) {
char line[100];
readingStream.getline(line, 100);
words[index] = _strdup(line);
}
}
int main()
{
string filename;
int count;
string line;
char *ch;
cin>>filename;
ch=new char[filename.length()];
for(int i=0; i<filename.length();i++)
ch[i]=filename[i];
read(ch,count);
ifstream myfile;
myfile.open(filename.c_str());
while(!myfile.eof())
{
getline(myfile,line);
cout<<line<<endl;
}
}