#Read a File

1 messages · Page 1 of 1 (latest)

eager cypress
#

I'm suppose to create a program that reads names one at a time. I'm not understanding what I am doing wrong I went online on YouTube to help me understand it thankfully, it was pretty straightforward and easy to understand, but every time I run the program, it says names and doesn't list the names in the file.

faint yewBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

keen wasp
eager cypress
#

This is what I have so far

#

And this is what happens when I run it

keen wasp
#

What is the name of the file?

eager cypress
#

Names.csv

keen wasp
#

And is that what you input to the program?

eager cypress
#

Im sorry after rechecking it says Names

keen wasp
#

It must be Names.txt

eager cypress
#

Hmmm when I open the file that my teacher had me download it opens excel

#

With the names

#

here

keen wasp
#

So is a csv?

eager cypress
#

Ok I probably explained this badly. When I click to down the file it says csv, but when I see it on the file, it says names. Does that matter?

keen wasp
#

Right to the name of the file

eager cypress
#

It says Excel in explorer

keen wasp
#

Show it

#

Im assuming its a csv

eager cypress
keen wasp
#

Input "Names.csv" to your progran

eager cypress
#

Ok one second

keen wasp
#

Without double quotes

eager cypress
#

Sorry this class is introduction to C++ and our class has to use visual studio

keen wasp
#

Its not dev c++

#

So did you enter the name of the file when your program asks for the input?

eager cypress
#

Yes but now there are errors and im not understanding what happened

#

Im checking if I hit a key I shouldn't have somewhere

#

Oh I see one second

keen wasp
#

You should just hardcode the name in the program, not ask for it in input as asshullbreaker mentioned.

eager cypress
#

How would I do that then?

keen wasp
#

change infile.open(Names); to infile.open("Names.csv"); and remove getline above it

eager cypress
#

Sorry I move slow

keen wasp
#

And also

The Names.csv should be next to your program exe or working directory

keen wasp
eager cypress
#

ok

#

Sure one second

#

The file is added and no errors now

#

Im sorry where?

keen wasp
#

That is easy to get around

#

First of all open the damn file, im sure it won't be easy cuz of visual studio

eager cypress
#

Click on it?

#

Because this is what happens

keen wasp
eager cypress
#

Oh hold on

keen wasp
#

Right

#

So now where is this csv file located, which folder

eager cypress
#

In my downloads?

#

Or is that not correct?

keen wasp
#

Copy it to the project directory

keen wasp
eager cypress
#

Got it give me a second again

#

Do you want me to put in the infile.open line?

keen wasp
eager cypress
#

Ok done

keen wasp
#

Now run

eager cypress
#

Epic

#

Hmm that small problem for that

#

ok

#

Thank you though

keen wasp
#

Lets move to the next problem

eager cypress
#

ok

keen wasp
#

The names are separated by ','

eager cypress
#

Oh yes that's right the other one did say that

#

So what exactly do I do?

keen wasp
#

You have to, as he said replace , with newline

eager cypress
#

As in just type newline?

#

Sorry still learning

keen wasp
#

Not really, you have to keep printing the character until you see that the character is comma, if its a comma dont print it instead print '\n' in its place, do you know how to do that?

eager cypress
#

No sorry

keen wasp
eager cypress
#

No

keen wasp
#

Giving you the solution directly will be spoon-feeding now, so maybe you should go through your code a line at a time, and figure out what exactly it is doing, otherwise you will learn nothing.

eager cypress
#

using namespace?

#

Thats whats separating it right or supposed to

keen wasp
#

No, it makes the names in std namespace visible globally, so you dont have to type std::cout, std::getline etc, but its a bad practice to put using namespace std

keen wasp
eager cypress
#

Would this be it? while (infile.get(c)) // Read and print each character from the file... Remembering from the video it reads each character correct?

keen wasp
#

Yes, this is what reads each character one at a time from the file

#

And what prints the character to the console?

eager cypress
#

cout

keen wasp
#

The character that is read from the file is stored in c defined above this while loop as char c;

keen wasp
#

You can check the value of c, if its ',' then print the newline

eager cypress
#

Went back to another video for a second that was like this but is it suppose to be like this?

keen wasp
#

Yes thats it

#

Did you get this from the video?

eager cypress
#

My teacher doesn't really have any videos on the class so im just researching what I can and trying to take notes at the same time

#

while not confusing myself

#

I found some helpful YouTube videos that explain c++ really well but get lost easily

#

Uh but however can I ask that we stop because it almost 3 here

eager cypress
#

Sorry and thank you

keen wasp
#

Ditch the videos get the book

keen wasp
eager cypress
#

Do you want me to close this and open another one? Or do you want me to leave this open when I come back

eager cypress
#

Ok then

hard agate
# eager cypress

What is ifstream? A decoration for file operations to can use ??

keen wasp
eager cypress
#

I believe that header is used to read files if im not mistaken

hard agate
eager cypress
#

Oh I was way off hahaha

#

night guys

hard agate
#

Nightt

eager cypress
#

!solved