?
Solved

Reading file with get().

Posted on 2003-03-12
14
Medium Priority
?
272 Views
Last Modified: 2010-04-01
Hi all,

I have a .txt-file with a random number (eg zipcodes, size is varying though) followed by a string (eg city name) on each line.
My intention is to read the file line per line and place the number in an int and the 'city name' in a string, to use both parameters in a constructor later on.
I'm not sure if I'm doing something wrong with the string pointer declaration or with the get()function..I was trying to have the number delimited by the space inbetween number and name on the file. I'm new at C++, programming altogether. Would anyone have any suggestion?


#include <iostream.h>
#include <fstream.h>

void main()
{
     ifstream in;
     ofstream out;

     char ch;
     char* zipcode;
     char* cityname;


     in.open("postcody.txt");
        while (!in.eof()) {
//          in.get(ch);
          in.get(zipcode, ' ');
          if (!in.eof())
          {
//               cout << ch;  
               out << cityname;
               in.close();

          }
     }

     in.close();
     cout << endl << "done" << endl;
}
0
Comment
Question by:Marty21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +4
14 Comments
 

Author Comment

by:Marty21
ID: 8118599
Actually I forgot to mention that the reason I wrote this is also because when I run this program as is above, executing it let's the console crash for some reason...

I'll convert the zipcode-string later on in an int. Is this a preferred way to do it?


0
 

Expert Comment

by:Ivaandr
ID: 8118698
#include <iostream.h>
#include <fstream.h>

void main()
{
    ifstream in;
    ofstream out;

    char ch;
    char zipcode[6];  //NOTE, that length of zipcode must be equivalent vith length from file
    char cityname[255];  //try 255, if length of cityname not more, than 255


    in.open("postcody.txt");
       while (!in.eof()) {
         in.get(zipcode,6);
         in.get(cityname,255);
         cout<<"zip:"<<zipcode<<";city: "<<cityname<<endl;
        }
    in.close();
    cout << endl << "done" << endl;
}
0
 
LVL 1

Expert Comment

by:madface
ID: 8118715
insted of char* for zipcode and cityname try
char zipcode[20];

your pointers where not initizied so they are pointing in odd spots. Also i beleive get() will not allocate memory for you so you must use an array.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Marty21
ID: 8118880
Actually I forgot to mention that the reason I wrote this is also because when I run this program as is above, executing it let's the console crash for some reason...

I'll convert the zipcode-string later on in an int. Is this a preferred way to do it?


0
 
LVL 1

Expert Comment

by:oferh
ID: 8118933
why dont you do:

in.open("postcody.txt");
      while (!in.eof()) {
         in >>zipcode >>cityname;
}
       
0
 

Author Comment

by:Marty21
ID: 8118937
(sorry for the earlier repeated comment, refreshing this page is not allowed it seems..)

So, using get(), I'm not able to let the program decide at runtime how many chars need to be read..? (in case of different zipcode sizes (US <-> European). That's why I was trying to figure out a way through pointers. I have tried to add
char* zipcode = NULL;

but that still gave the execute-crash..
0
 

Author Comment

by:Marty21
ID: 8119014
I tried that oferh,
it still crashes on me..
Doing that does that mean it automatically skips all whitespaces?

0
 
LVL 12

Expert Comment

by:Salte
ID: 8120205
One minor point.

Not sure if it is smart to specify zip code as number. For US and possibly for most countries there are numeric codes for this kind of thing, but they often have letters or something around them. For example in Norway we use a 4 digit number as zip code but if sending from abroad we sometimes prefix that 4 digit number with "N-" indicating 'Norway' so a zipcode of 1234 would be N-1234. Such a zip code would crash if you require the zipcode to be a pure number.

I think the safest bet is to just treat the zipcode like a string just like the city name. It just often happen to be a number but that is irrelevant. If you want an integer for fast lookup, use a hash function from the zipcode string and use the integer value from that hashfunction as your integer value.

Alf
0
 

Author Comment

by:Marty21
ID: 8121855
It's more that I'm trying to challenge myself with goals I set forward for myself.
The idea was to arrange citynames using the postalcodes int value (in ascending order), eventually using balanced trees..I have a long way to go, but it kinda bothers me that I'm losing time trying to figure out why I get this error when I execute.

"reading.exe has encountered a problem and needs to close. We are sorry for the inconvenience. (with of course no further explanation really except some dump code)"

I was using postal codes coz I had a file already written in that format. But in the end I didn't want to restrict myself to just the 6 numbers of the USA...I wanted eventually to use a file that could have numbers from 1-1 million or so. That's why I was hoping using pointers would've helped me out. But according to this error message it won't let me.
You're right I was going to use the zipcode as string and then convert it to int. As for hashfunction, I have read something about hashtables but I haven't been quite into that. If I can't find no answer on the net here I guess I'll have to change my goals, like you said, but in my eyes it would be a shame not to learn why it didn't work :).
0
 
LVL 1

Accepted Solution

by:
jadams117 earned 300 total points
ID: 8124342
I think the code below solves your problem. It's better to use the std::string class to hold strings if you don't know their length beforehand as it will be allocated dynamically as needed. Your crash was caused by the uninitialised char* pointers..

#include <fstream>
#include <iostream>
#include <string>
#include <conio.h>

int main()
{
     std::ifstream inFile("temp.txt");

     std::string zip;
     std::string city;

     while (!inFile.eof())
     {
          inFile >> zip >> city;
          std::cout << zip << ' ' << city << '\n';
     }

     while (!kbhit());
     return 0;
}
0
 
LVL 2

Expert Comment

by:DarthNemesis
ID: 8124400
Do you have any objections to working with a string object? Character arrays are more efficient when your data is strictly organized, but it's a pain to try to allocate them correctly when you don't know how long they have to be.

BTW, your program was crashing becase you declared the variables as pointers but didn't allocate any memory for them, i.e. char* zipcode = new char[5].

Anyway, here's an example using strings that works fine:

#include <iostream.h>
#include <fstream.h>
#include <string.h>
using namespace std;

void main()
{
        ifstream in("postcody.txt");
        string zipcode, cityname;
        while (!in.eof()) {
                in >> zipcode >> cityname;
                // add storage operations here
                cout << zipcode << " " << cityname << endl;
        }

        in.close();
        cout << endl << "done" << endl;
}

And yes, the extraction operator >> ignores whitespace.
0
 
LVL 1

Expert Comment

by:jadams117
ID: 8124415
By the way:

The std:: parts indicate that the names 'ifstream', 'string' and 'cout' lie within the std namespace. This namespace is a convention used within the standard c++ libraries to prevent name-clashes in case you have any other objects with those same names

The line 'while (!kbhit());' is just a way of waiting for a keyboard event so that the DOS window doesn't close before you've read what's on the screen
0
 

Author Comment

by:Marty21
ID: 8128174
Thanks for your replies, I'm glad there are people like you willing to help people like me..:)
I was still wondering..

suppose the cityname has whitespaces still in its name (before the end of the line, but that I still need, like eg "Los Angeles"), how do I avoid the ifstream in to consider Angeles as a zipcode?
I've been trying things like (among others)
in.get(cityname,255,'\n');
but that didn't work..any ideas still?


(Also, I feel like both DarthNemesis and jadams117 should get points because both of you really helped me, but I'm afraid I can only give one of you points right (is there any way around this?)..I'll just use the time-factor then..(sorry DarthNemesis..can I reward you later on?))
0
 
LVL 2

Expert Comment

by:DarthNemesis
ID: 8131249
All you should have to do is change one of the >> operators to getline:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void main()
{
       ifstream in("postcody.txt");
       string zipcode, cityname;
       while (!in.eof()) {
               in >> zipcode;
            getline(in, cityname);
               // add storage operations here
               cout << zipcode << " " << cityname << endl;
       }

       in.close();
       cout << endl << "done" << endl;
}

As for giving me points too, thanks, you can just start a new problem with a title like "Points for DarthNemesis" and I'll respond to it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question