473,388 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Confusion with homework .. infile

21
So with infile I have this so far:

#define MAXBOOKS 9
ifstream infile("library.txt");
struct Book
{
title[MAXBOOKS]
}
while(true)
{
for(int i = 0; i < MAXBOOKS; i++)
{
char tempTitle[MAXBOOKS];
infile.getline(book.title, MAXLENGTH);
}
}

I also have the main function, but my question is with the getline. My problem is how do i get the book.title to go into the title[0].
if i try to declare it by setting book.title[0] = book.title, I come up with an error obviously.. but how do i get the infile into book.title[0]????
Sep 24 '06 #1
15 4829
Expand|Select|Wrap|Line Numbers
  1. struct Book
  2. {
  3.     Title[MAXBOOKS];
  4. };
  5.  
  6. int main() 
  7. {
  8.     Book myBooks;
  9.     int i = 0;
  10.  
  11.     ifstream infile("library.txt");
  12.  
  13.     for (i=0;i<MAXBOOKS; i++)
  14.         infile.getline(myBooks.Title[i], MAXLENGTH);
  15.     return 0;
  16. }
  17.  
Sep 24 '06 #2
jjh
21
Will that work if i am bringing in a a name of a book ex. "Principles of Chemistry", or will i have to use a 2d array ????
Sep 24 '06 #3
jjh
21
this is a test
Sep 25 '06 #4
you don't have to use 2D arrays.
You can pass a number to that array and it'll give you the book title back.
What i don't understand is why you're using structs at all.
Unless of course you're planning to save more stuff in that struct.
Sep 25 '06 #5
jjh
21
string tempTitle;
infile.getline(tempTitle,MAXLENGTH);
If I put the infile in for a string and then assigning it into a char array it doesn't work.

I get this error

c:\documents and settings\owner\my documents\visual studio 2005\projects\hw 4\hw 4.cpp(34) : error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_El em *,std::streamsize)' : cannot convert parameter 1 from 'std::string' to 'char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

CAN YOU THINK OF SOMETHING

this is what I have so far.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
#define MAXBOOKS 9
#define MAXKEYWORDS 3
#define MAXLENGTH 50
struct Book
{
char title[MAXBOOKS];
char keyWord[MAXKEYWORDS][MAXLENGTH];
char position;
};
int fillArray()
{
char title;
char keyword1;
char keyword2;
char keyword3;
Book theBook;
ifstream infile("library.txt");
while(true)
{
for(int i = 0; i < MAXBOOKS; i++)
{
// infile >> theBook.title[i];

string tempTitle;
infile.getline(tempTitle,MAXLENGTH);
//strcpy(theBook.title[i], tempTitle);
cout << theBook.title[i];

for(int j = 0; j < MAXKEYWORDS; j++)
{

}
}
}
return 0;
}

plus a main function
Sep 25 '06 #6
Banfa
9,065 Expert Mod 8TB
This
Expand|Select|Wrap|Line Numbers
  1. struct Book
  2. {
  3.     Title[MAXBOOKS];
  4. };
  5.  
is not a proper definition because Title has no type. It probably ought to be something like

Expand|Select|Wrap|Line Numbers
  1. #define MAXBOOKS              500  // Set these values yourself
  2. #define MAXTITLE                   50
  3.  
  4. struct Book
  5. {
  6.     char Title[MAXTITLE];
  7. };
  8.  
in which case main should be

Expand|Select|Wrap|Line Numbers
  1. int main() 
  2. {
  3.     Book myBooks[MAXBOOKS];
  4.     int i = 0;
  5.  
  6.     ifstream infile("library.txt");
  7.  
  8.     for (i=0;i<MAXBOOKS; i++)
  9.         infile.getline(myBooks[i].Title, MAXTITLE);
  10.     return 0;
  11. }
  12.  
Sep 25 '06 #7
jjh
21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
#define MAXBOOKS 9
#define MAXKEYWORDS 3
#define MAXLENGTH 50
struct Book
{
char title[MAXBOOKS];
char keyWord[MAXKEYWORDS][MAXLENGTH];
char position;
};
int fillArray()
{
char title;
char keyword1;
char keyword2;
char keyword3;
Book theBook;
ifstream infile("library.txt");
while(true)
{
for(int i = 0; i < MAXBOOKS; i++)
{
// infile >> theBook.title[i];

string tempTitle;
infile.getline(tempTitle,MAXLENGTH);
//strcpy(theBook.title[i], tempTitle);
cout << theBook.title[i];

for(int j = 0; j < MAXKEYWORDS; j++)
{

}
}
}
return 0;
}

This is what I have but it doesn't work can you inform me there is something wrong with me infile but I dont know what.
Sep 25 '06 #8
Banfa
9,065 Expert Mod 8TB
This is the error

Expand|Select|Wrap|Line Numbers
  1. string tempTitle;
  2. infile.getline(tempTitle,MAXLENGTH);
  3.  
getline does not work on type string, it works on array of char.

If you are going to have more than 1 book you need an array of Book structures (or a linked list but let's not complicate things) and finally you have declared you book local to FillArray so as soon as you exit the data will be gone

Expand|Select|Wrap|Line Numbers
  1. Book Books[MAXBOOKS];
  2.  
  3. int fillArray()
  4. {
  5.     ifstream infile("library.txt");
  6.  
  7.     for(int i = 0; i < MAXBOOKS; i++)
  8.     {
  9.         infile.getline(Books[i].title, sizeof Books[i].title);
  10.         cout << Books[i].title;
  11.  
  12.         for(int j = 0; j < MAXKEYWORDS; j++)
  13.         {
  14.             infile.getline(Books[i].keyWord[j], sizeof Books[i].keyWord[j]);
  15.         }
  16.     }
  17.  
  18.     return 0;
  19. }
  20.  
Sep 25 '06 #9
duh, you're incrementing the struct array, not the .title lol. I'm such an idiot.
Sep 25 '06 #10
jjh
21
I tried this :
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < MAXBOOKS; i++)
  2.         {
  3.  
  4.             string tempTitle;
  5.             infile.getline(theBook[i].title ,MAXLENGTH);
  6.             cout << theBook[i].title;
  7.                  }
  8.  
and then i got the error

c:\documents and settings\owner\my documents\visual studio 2005\projects\hw 4\hw 4.cpp(33) : error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_El em *,std::streamsize)' : cannot convert parameter 1 from 'char' to 'char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

The message i am trying to bring it is "Principles of chem"
Sep 25 '06 #11
Banfa
9,065 Expert Mod 8TB
Not if you are using the structure

Expand|Select|Wrap|Line Numbers
  1. struct Book
  2. {
  3.   char title[MAXTITLE];
  4.   char keyWord[MAXKEYWORDS][MAXLENGTH];
  5.   char position;
  6. };
  7.  
  8. struct Book theBook[MAXBOOKS];
  9.  
you aren't so I suspect you aren't doing that or you haven't change the code correctly.
Sep 25 '06 #12
jjh
21
Ok i got that alright and thank you, but one other thing...
The file I am wanting to bring in, is read like this...

Title
keyword
keyword
$

my problem now is how do I make the key words is a single array... Is there a way to call theBook[i].keyword[j] and have j as a inside for loop???
Sep 25 '06 #13
Banfa
9,065 Expert Mod 8TB
See this post

http://www.thescripts.com/forum/post2107947-9.html

that I have already made to this thread.
Sep 25 '06 #14
jjh
21
Oh I am sorry i didn't even see it. Thank you so much.
Sep 25 '06 #15
jrjan1
7
Can I ask a question on this thread?

What if you have no idea how big the infile is? How do you set MAXBOOKS and MAXLENGTH?
Dec 8 '06 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Bruce A. Julseth | last post by:
When I execute this SQL statement in my PHP code, I get an error "File '.\Address.txt' not found (Errcode: 2)" $File = addslashes(".\Address.txt"); $SQL = "Load Data InFile \"" . $File . "\"...
7
by: las | last post by:
I'm having a wee problem with the get method, here is my code : ifstream infile; char x; infile.open("temp.txt"); if( !infile.good() ) { cout << "Error opening file" << endl; system("PAUSE");...
0
by: Donald Tyler | last post by:
Then the only way you can do it that I can think of is to write a PHP script to do basically what PHPMyAdmin is trying to do but without the LOCAL in there. However to do that you would need to...
0
by: Steffen | last post by:
Hello, I´m using mysql 3.23.58 and I want to enable the local-infile option. The manual on dev.mysql.com says the following to that: ..... If you use LOAD DATA LOCAL in Perl scripts or other...
1
by: Ray in HK | last post by:
What are the differences between LOAD DATA INFILE and LOAD DATA LOCAL INFILE ? I found some web hosting company do not allow using LOAD DATA INFILE but allow LOAD DATA LOCAL INFILE. The reason...
2
by: Jason3231 | last post by:
well i've found and pieced together a script (mainly found; i'm very new to perl) to scan a group of cisco switches that i have and print information out in a text file. everything seems to work...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
2
by: jjh | last post by:
Trying to input a file say file.txt The first line I am trying to input is: Principles of Biochemistry Trying to put "Principles of Biochemistry" into a char array book.title with a max of...
15
by: waltbrad | last post by:
Hello. I'm studying the book "C++ Primer Plus" by Stephan Prata. In chapter 6 he gives an exercise that reads from a file. The list is thus: 4 Sam Stone 2000 Freida Flass 100500 Tammy...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.