Help please for (me) reading a text file I open

I need help reading from a text file that I opened. What the result is looks like a list of what I think are the address of the data in file

//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;

//Prototypes
//int data_file_open(ifstream & file_buffer); // initialize function to open files

int data_file_open (ifstream & file_buffer)
{
string programmer;
string storedfile; // blank string declared
cout << "hello! what is your name?!" <<endl;
getline (cin,programmer);
cout << "Which file does " << programmer << " wish to open?!\n"; //prompt user to open file
getline (cin,storedfile); // takes user's input and stores into storedfile
file_buffer.open (storedfile); // opens file if file exist
if (file_buffer.fail())
{
cout<< "Filename not found " << programmer << ". Check your spelling\n";
return 0;
}
cout << programmer << ", your file has been opened " << endl;
//file_buffer.close();
return 1;
}


///// fills an array ////////

int fill_array_from_file (unsigned int ARRAY_SIZE, ifstream &file_buffer, double array1[])

{
int counter= 0;


while (!file_buffer.eof() && counter<ARRAY_SIZE)
{
file_buffer >> array1[counter]; counter++;
cout << file_buffer << endl;
}

return counter;
}



int _tmain(int argc, _TCHAR* argv[])
{

ifstream file_buffer;
data_file_open(file_buffer); //calling function data file open
double array1[100];

unsigned int ARRAY_SIZE=100; // size large enough to hold all data in array
cout << fill_array_from_file (ARRAY_SIZE, file_buffer, array1);

system ("PAUSE");

return 0;

}


/*


cout << storedfile;
//data_file_open();
ifstream file_in (storedfile);
file_in.close(); // always close file before exiting



int result=0;*/
Last edited on
Maybe you need something more like this:
(changed array1 to char array, because chars are what you get from file.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int fill_array_from_file (unsigned int ARRAY_SIZE, ifstream &file_buffer, char array1[])
{
	int counter= 0;
	while (!file_buffer.eof() && counter<ARRAY_SIZE)
	{
		array1[counter++] = file_buffer.get(); // get next letter from file and ++ counter
		cout << array1[counter-1]; // cout one letter at a time
                                         // could also be done after the loop
		// cout << file_buffer << endl; // not cout file_buffer address 100 times
	}
	cout << endl;
	cout << array1 << endl; // cout entire array once
	return counter; 
}
Last edited on
thanks here is what i came up with

// Lab6_W01045456.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>

using namespace std;


string nullstring;
int start_pos = 0;



//word start function*************************************************


int position_next (int start_pos, string nullstring)
{
if (start_pos >= nullstring.length())

return -1;

////// array[i] == ' ' ////////////
while (nullstring[start_pos] == ' ') //check the variable in textstring whether is blank
{
start_pos++;
if (start_pos>= nullstring.length()) //start position cant' be larger than array or text length
return -1;
}

return start_pos; // returns the array element /// position of the start
}


// word_length function

int word_length(int start_pos, string nullstring)
{
while (nullstring[start_pos] != ' ') //check the variable in textstring whether is still a chararcter
{
start_pos++; //increment until start_pos reaches a char
cout << start_pos<<endl;
if (start_pos>= nullstring.length()) //start position cant' be larger than array or text length
return -1;

}
return start_pos; // returns the array element /// position of the start
}



///parse140///
/*string pp_breaker(string my_string[], int ARRAY_SIZE, string nullstring, int listlength) // breaks paragraph into words function

{

start_pos= 0, my_string[]


}*/

int _tmain(int argc, _TCHAR* argv[])
{
int o =0;
string TEST("What time "); //begin of testimg program functions
cout<<"enter words"<<endl;

getline (cin ,TEST);
position_next (o, TEST);

o = position_next (o, TEST);
cout<<o<<endl;


word_length(o, TEST);
o = word_length(o, TEST);

cout<<o<<endl;
cout<< TEST<< endl;
system ("pause");


return 0;
}
Topic archived. No new replies allowed.