Hello everyone.
I am trying to read from a text file using getline in c++, but I am getting additional data on top of the output I'm looking for. I am pretty new to c++ and am trying to populate a structure using input from a "txt" file. Below is my following code and the result I'm getting. At the end of the result it says "sample". That is the only thing I am trying to get output by cout. How do I rid of all the other information that is printing? I also tried using printf but as you could guess I was getting the same result. Also one other question I have is it possible to change where Xcode looks for your txt file? I would like for it to read in the same folder as main.cpp if possible.
Thank you,
Jonathan
My output:
{\rtf1\ansi\ansicpg1252\cocoartf1187\cocoasubrtf340{\fonttbl\f0\fswiss\fcharset0 Helvetica;}{\colortbl;\red255\green255\blue255;}\margl1440\margr1440\vieww10800\viewh8400\viewkind0\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\f0\fs24 \cf0 sample}
I am trying to read from a text file using getline in c++, but I am getting additional data on top of the output I'm looking for. I am pretty new to c++ and am trying to populate a structure using input from a "txt" file. Below is my following code and the result I'm getting. At the end of the result it says "sample". That is the only thing I am trying to get output by cout. How do I rid of all the other information that is printing? I also tried using printf but as you could guess I was getting the same result. Also one other question I have is it possible to change where Xcode looks for your txt file? I would like for it to read in the same folder as main.cpp if possible.
Thank you,
Jonathan
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
struct textInput {
string name;
string id;
string security;
string dob;
} line01, line02, line03, line04;
void printOut(textInput data);
int main()
{
ifstream myfile ("sample.txt");
if (myfile.is_open())
{
while ( !myfile.eof() )
{
getline (myfile, line01.name);
printOut(line01);
}
myfile.close();
}
else
cout << "File could not be openend";
return 0;
}
void printOut(textInput data){
cout << data.name;
}
My output:
{\rtf1\ansi\ansicpg1252\cocoartf1187\cocoasubrtf340{\fonttbl\f0\fswiss\fcharset0 Helvetica;}{\colortbl;\red255\green255\blue255;}\margl1440\margr1440\vieww10800\viewh8400\viewkind0\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\f0\fs24 \cf0 sample}