• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

Newbie question.. How do i read from file?

I am sort of new to C++.. I am trying to do my assignment, but I don't know how to read a file to input.. can someone give me an idea of how to do it?

I think it has to do with #include <fstream> (or <ifstream>?) i'm not sure which one.. and i know there are many ways to do it so..

How do i read a txt file?  say the phone_book.txt file contains a list of phone numbers, xxx-xxx-xxxx?
0
SuperSid
Asked:
SuperSid
  • 9
  • 2
  • 2
  • +1
1 Solution
 
mactep13Commented:
You will need an fstream object:

#include <fstream>

using namespace std;

fstream file;

// Open the file by calling open func
file.open("FileName.txt", ios::in);

// check id the file has opened.
if (file.is_open())
{
         char szBuffer[256];
         // Get one line of text (Until New Line Char)
          file.get_line(szBuffer, 256);
        // do something with the string
}
else
{
    // Display error
}
// don't forget to close the file
0
 
mactep13Commented:
Sorry, the get_line call should be:

file.getline(szBuffer, 256);

The number you are giving is the max num of chars to read. If the line is bigger than 256 chars, it will be truncated. The reason we're giving 256 is because it is the size of the char array. If you think you will need bigger array, declare it as char szBuffer[512] or whatever size you think you need.

Hope this helps,

Mactep.
0
 
SuperSidAuthor Commented:
thanks I will test it out.

what does "ios::in" mean?

and i'm assuming char szBuffer[256] is the size of char?

and how do i close the file hehe
0
Independent Software Vendors: 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!

 
beryl666Commented:
ios::in means input which you get the inpur from your file.
>>char szBuffer[256] is the size of char?
nope. it is the size of string that you input which is a line of characters.
close file? file.close();
0
 
SuperSidAuthor Commented:
cool the codes work, but how do i work the file.get_line?

say i want to read a file containing:

915-253-2538
626-235-3241

etc etc

i need to put the area codes into an int, prefix into one, and suffix into one..
so i need to create an

int areacode, prefix, suffix;

but how do i actually put those area codes into the "areacode" and prefix into prefix, suffix into suffix?

is there a way to break them down into 3 parts, if this isn't too confusing..
0
 
SuperSidAuthor Commented:
i think it has something to do with this code?

ifstream infile; (or fstream infile?)

int area,pre,suf;

while(!infile.eof())
{
    infile >>area
    infile >>pre
    infile >>suf
}

something like this? haha i have no clue ><
0
 
Mercurius0Commented:
You can use fopen in combination with fscanf

#include <stdio>

FILE *fp1;
int output[3];

fp1=fopen(phone_book.txt, "r");
fscanf(fp1, "%d-%d-%d", &output[0], &output[1], &output[2]);
fclose(fp1);
 
The three parts of the phone number will be in output[0], [1] and [2]
remember to call fclose, its important.
0
 
SuperSidAuthor Commented:
hehe so wat exactly does that do?

ok so the codes work and all, i know that output[0], 1, and 2 are the 3 parts to "a" phone number.

but the phone_book.txt has about 10 lines if phone numbers..

so how do i say take 1 of the phone number, seperate them into 3 parts, then call into another function to add the phone number into phone book(i did this part), THEN do a 2nd phone number, add this to the phone book, do a 3rd phone number, etc, etc... if u know wat i mean

well i'm going to have to use a while loop, but i have no clue where to put it in ur code
0
 
SuperSidAuthor Commented:
sorry last question was probably confusing, here:

FILE *fp1;
  int output[3];
  ifstream FileIn;

  fp1=fopen(fileName.c_str(), "r");

  fscanf(fp1, "%d-%d-%d", &output[0], &output[1], &output[2]);
  fclose(fp1);

  InsertPhoneNmber(output[0],output[1],output[2]);  //but this only inserts it once

do i put a while loop before fscanf? i'm not at all familiar with these codes hehe
0
 
Mercurius0Commented:
FILE *fp1;
  int output[3], i;
  ifstream FileIn;

  fp1=fopen(fileName.c_str(), "r");

for(i=0;i<number of phone-numbers;i++)
{
  fscanf(fp1, "%d-%d-%d", &output[0], &output[1], &output[2]);
  InsertPhoneNmber(output[0],output[1],output[2]);  //but this only inserts it once
}
fclose(fp1);

if you don't now the number of phone-numbers use

FILE *fp1;
  int output[3];
  ifstream FileIn;

  fp1=fopen(fileName.c_str(), "r");

while(getc()!=EOF)
{
  ungetc();
  fscanf(fp1, "%d-%d-%d", &output[0], &output[1], &output[2]);
  InsertPhoneNmber(output[0],output[1],output[2]);  //but this only inserts it once
}
fclose(fp1);
0
 
SuperSidAuthor Commented:
I compiled the 2nd one and got the following error:


/usr/include/stdio.h: In member function `void
   PhoneBook::readFromFile(std::basic_string<char, std::char_traits<char>,
   std::allocator<char> >)':
/usr/include/stdio.h:438: too few arguments to function `int getc(FILE*)'
phone_book.cc:41: at this point in file
/usr/include/stdio.h:598: too few arguments to function `int ungetc(int, FILE*)
   '
phone_book.cc:43: at this point in file
0
 
SuperSidAuthor Commented:
replying to the very first post from mactep13:

fstream file;

// Open the file by calling open func
file.open("FileName.txt", ios::in);

// check id the file has opened.
if (file.is_open())
{
         char szBuffer[256];
         // Get one line of text (Until New Line Char)
          file.get_line(szBuffer, 256);
        // do something with the string
}
else
{
    // Display error
}



this code seemed to work well after i did some twist to it with stringstream... but the problem is, the file.getline only reads 1 line..

how do i make it read the next line?  


Also, regarding the points, I can give to multiple users right? just make another post with 50 points and make someone else's reply to be an answer and get the extra points?
0
 
SuperSidAuthor Commented:
Mercurius0 ,

You don't need to answer my last question hehe, fortunately I do know how many #'s are in phone_numbers.txt, there are 26 lines of #'s.
0
 
SuperSidAuthor Commented:
I got it to work now, my final codes are:

FILE *fp1;
  int output[3], i;
  ifstream FileIn;

  fp1=fopen(fileName.c_str(), "r");

  for(i=0;i<26;i++)
    {
      fscanf(fp1, "%d-%d-%d", &output[0], &output[1], &output[2]);
      insertPhoneNumber(output[0],output[1],output[2]);
    }
  fclose(fp1);



thx a lot everyone!!  I will give both Mercurious0 and mactep13 points; it seems like mactep13 isn't reading this too much, so I will make a new post to give Mercirious0 50 points and accept mactep13's answer as an accepted answer?
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 9
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now