cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : Not reading .txt file
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programm...
Articles
Lounge
Jobs

-

post  Not reading .txt file

Vector (24)
I have this code, and for some reason its not reading the text file..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

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

using namespace 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

| Last edited on
Zhuge (110)
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().
|
Bazzy (539)
You have (starting from line 34) this code:
1
2
3
4
5
6
7
8
9
10
11
12
	cout << "Enter a valid filename: ";
	cin >> theFile; 

	
	if ( !inFile )
	{
		cerr << "This is wrong" << endl;
		exit(1);
	}

	inFile.open("c:\\test.txt");
	inFile.open ( "test.txt", ios::out );

But it tests inFile before opening it (so will always fail).
After that inFile opens twice.
| Last edited on
Vector (24)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 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>

using namespace 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?
|
Bazzy (539)
You need to give the number of elements to a vector:
vLineList.resize(/*number of elements*/); before using it.
|
Zhuge (110)
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.
|
firedraco (847)
The only time you will HAVE to use it is when you are trying to have blank elements, i.e. vector with 3 elements, and you want to access element 5.
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2009 - All rights reserved - v2.2
Spotted an error? contact us