#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
usingnamespace std;
struct myStruct
{
int myIdent;
float myPrice;
float myMarkup;
};
char g_userOption;
char *g_myPtr; // declaring my Global Varries
int g_myFindID;
int main ( int argc, char** argv )
{
fstream inFile; // using inFile as my file stream varaible
string sLine, theFile; // using sLine as a string varrrie
vector < string > vLineList;
char myFile [ 1000 ];
struct myStruct myProds [ 1024 ]; // declaring structure identifier that i can use
char myStr[ 1024 ];
char digits [ 1024 ];
cout << "Enter a valid filename: ";
cin >> theFile; // <-this is probably whats wrong
if ( !inFile )
{
cerr << "This is wrong" << endl;
exit(1);
}
inFile.open("c:\\test.txt");
inFile.open ( "test.txt", ios::out ); // i need the files output
while ( getline ( inFile, sLine ) )
{
if ( sLine.length() == 0 ) continue;
vLineList.push_back ( sLine );
for ( int myNums = 0; myNums < vLineList.size(); ++myNums)
{
if ( sLine [ myNums ] == ',' ) break;
}
for ( int myDecs = 0; myDecs < vLineList.size(); ++myDecs )
{
if ( digits [ myDecs ] == ',' ) break;
}
}
inFile.close();
cout << sLine << endl;
system("pause");
return 0;
}
from what i can remember the contents of the file are stored in sLine;
I dont want my program to read only, test.txt but read every .txt file on the system
If you are including string, you might as well get rid of all the char[] arrays and just use std::strings. Any place you need a char array, just call the member function c_str().
// FILEReader.cpp : Defines the entry point f
// fileIO.cpp : Defines the entry point for the console application.
//
// ReadFile.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
usingnamespace std;
struct myStruct
{
int myIdent;
float myPrice;
float myMarkup;
};
char g_userOption;
char *g_myPtr; // declaring my Global Varries
int g_myFindID;
int main ( int argc, char** argv )
{
fstream inFile; // using inFile as my file stream varaible
filebuf *myBuf;
string myFile; // using sLine as a string varrrie
string sLine = "";
vector < string > vLineList;
struct myStruct myProds [ 1024 ]; // declaring structure identifier that i can use
char myStr[ 1024 ];
cout << "Enter a valid filename: ";
cin >> myFile;
inFile.open( myFile.c_str(), ios::in | ios::binary);
if ( !inFile ) // validate thefile
{
cerr << "This is wrong" << endl;
return 1;
}
char bytes [ 4 ];
inFile.read ( bytes, 4 );
while ( getline ( inFile, sLine ) )
{
if ( sLine.length() == 0 ) continue;
vLineList.push_back ( sLine );
inFile.seekg ( 0, ostream::end ); // telling me how many lines inFile.
cout << "Position is" << inFile.tellg() << endl;
cout << sLine << endl;
}
inFile.close(); // close the file
system("pause");
return 0;
}
I was able to read the file properly...but what is my code doing?
has the data been allocated by using the vector <string> vLL?
Do i still need to use new, and delete to allocate further?
You do not have to use resize, nor do you have to tell the vector how large to be. std::vector automatically handles allocating new space as you add elements.