1 Replies - 235 Views - Last Post: 13 January 2013 - 02:05 PM Rate Topic: -----

#1 BolideRider  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 11-April 11

cout is giving me unexpected result

Posted 13 January 2013 - 01:50 PM

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


#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}

Is This A Good Question/Topic? 0
  • +

Replies To: cout is giving me unexpected result

#2 BolideRider  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 11-April 11

Re: cout is giving me unexpected result

Posted 13 January 2013 - 02:05 PM

Okay so I figured out what the problem was. When I looked closer at my output I saw the "rtf" and realized my txt files was actually rich text format. So that fixes it. If anyone can answer my question about selecting where Xcode looks for you txt document though then that would be great! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1