Accessing .txt files with inFile

Hello! I am fairly new to coding in general and even newer to C++. I need to write a code that accesses a .txt file, which just has a bunch of type double numbers in it, do some stuff with the numbers, and then rewrite it in a different way. I'll figure the other stuff out on my own, but I keep getting an error when I try to read the .txt file to begin with. Here is what I am using:

 
     ifstream inFile("c:numberData.txt", ios::in);


The .txt file named "numberData.txt" (I actually typed out .txt at the end) is under the same folder as the rest of the Visual Studios stuff. Whenever I try to run the program, it says it can't access it. I feel like this is a slight syntax error or where I put the .txt file but I've been messing with it for awhile now and have gotten nowhere. Suggestions?
What version of Visual Studio are you using?

Whenever I try to run the program, it says it can't access it.

What is saying "it can't access it"? And what is "it"?

How and where did you create your out.txt file.

Are you sure Windows didn't help you by adding the ".txt" for you (making the file name numberData.txt.txt)?

I am using Visual Studios 2019 Version 16.4.3.

When I try to run the program it says "There were build errors. Would you like to continue and run the last successful build?" (I am not sure how it says this because it's a new program). Once I click yes it pulls up another error that says, "The system cannot find the file specified." I am not sure what the "file specified" is, whether its the program itself or the .txt file. I made the .txt file with Windows Notepad and "saved as" "numberData.txt".
(I am not sure how it says this because it's a new program)


"There were build errors.


This means that there are some syntax errors in your code that need to be fixed before the program can run.

Once I click yes it pulls up another error that says, "The system cannot find the file specified." I am not sure what the "file specified" is, whether its the program itself

Because this is a "new" project the executable was never created because of the errors. The "file specified" is the executable which hasn't yet been created. So instead of answering "yes" say "no".

Next make sure you created a project inside your IDE and are not just trying to use a source file.

The development environment should be showing you the error messages, somewhere. You need to find them and post them and post your code.

Last edited on
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
#include <iostream>
#include <fstream>

using namespace std;


//main function
int main()
{
	ifstream inFile("c:datafile", ios::in);
	double inValue;
	int size = 0;
	bool fail = 0;
	//find the size of the data file
	while (fail == 0)
	{
		inFile >> inValue;
		fail = inFile.fail();
		if (fail == 0) size = size + 1;
	}
	inFile.clear();
	inFile.seekg(0, ios::beg);
	double* data;

	for (int n = 0; n < size; n++) inFile >> data[n];
	for (int n = 0; n < size; n++) cout << "data[" << n << "]\t" << data[n] << endl;
	system("pause");
	inFile.close();
	return 0;
}


This doesn't actually accomplish what I need it to but I used this to get it to work.

I am not sure what you mean by...

Next make sure you created a project inside your IDE and are not just trying to use a source file.

I am very new to the terminology but above my code the tab says "Source.cpp".
Hello JoeyB190,

First thing I notice is ifstream inFile("c:datafile", ios::in). "c:" is the start of trying to say that the file resides in the main directory of "c" drive. The file should be in the current working directory, i.e., the same place of the ".cpp" files, and loose the "c:". Then it should fine the input file.

Speaking of the input file please post the input file or if large a fair sample so that everyone is using the dame information. It also helps to see the layout to know if you are reading the file correctly.

Next is the part ifstream inFile("c:datafile", ios::in). "ifstream" implies an input file stream and "ofstream" implies an output file stream. Only if you define the stream as a "fstream" you need to tell it if the file is for input, output or both.

In your while loop you have if (fail == 0) size = size + 1;. If yo want you could read the file, store the information and count how many times you store a variable. Actually a vector would be a better choice than an array.

The line double* data; // <--- Uninitialized variable. Contains garbage. Also a pointer not an array. Following this the "new' key word would be used to create an array on the heap. If you use new be sure to use "delete" to free the memory when finished using it.

I am not sure I am grasping what you are trying to do, but I believe I have guessed at most of it.

The while loop is to read how many numbers are in the file so that you can create a dynamic array. Using a vector would eliminate the need for this while loop. This could be done in the first for loop.

Andy
I'd recommend you start with something like:
1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
    std::cout << "Hello World" << std::endl;

}


Then compile and run the program to insure your development system is properly set up.

One you get this to work start adding code one line at a time, compiling often (after as few as one line), fixing any warnings and errors as you go.

Hello JoeyB190,

When working on your program I noticed: ifstream inFile("c:datafile", ios::in);, but how do you know it is open?

What you need is:
1
2
3
4
5
6
7
8
ifstream inFile("datafile.txt"/*, ios::in*/);  // <--- "ifstream" already implies an input file.

if (!inFile)
{
	std::cout << "\n    File \"datafile.txt\" did not open!\n";

	return 1;
}


Also since you are using floating point numbers this line helps:
std::cout << std::fixed << std::showpoint << std::setprecision(4); // <--- Only needs done once. . Needs the header file "iomanip".

Andy
Topic archived. No new replies allowed.