jjh 21
New Member
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]????
15 4852 -
struct Book
-
{
-
Title[MAXBOOKS];
-
};
-
-
int main()
-
{
-
Book myBooks;
-
int i = 0;
-
-
ifstream infile("library.txt");
-
-
for (i=0;i<MAXBOOKS; i++)
-
infile.getline(myBooks.Title[i], MAXLENGTH);
-
return 0;
-
}
-
jjh 21
New Member
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 ????
jjh 21
New Member
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.
jjh 21
New Member
string tempTitle;
infile.getline( tempTitle,MAXLE NGTH);
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\visua l studio 2005\projects\h w 4\hw 4.cpp(34) : error C2664: 'std::basic_ist ream<_Elem,_Tra its> &std::basic_ist ream<_Elem,_Tra its>::getline(_ Elem *,std::streamsi ze)' : cannot convert parameter 1 from 'std::string' to 'char *'
with
[
_Elem=char,
_Traits=std::ch ar_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,MAXLE NGTH);
//strcpy(theBook. title[i], tempTitle);
cout << theBook.title[i];
for(int j = 0; j < MAXKEYWORDS; j++)
{
}
}
}
return 0;
}
plus a main function
Banfa 9,065
Recognized Expert Moderator Expert
This -
struct Book
-
{
-
Title[MAXBOOKS];
-
};
-
is not a proper definition because Title has no type. It probably ought to be something like -
#define MAXBOOKS 500 // Set these values yourself
-
#define MAXTITLE 50
-
-
struct Book
-
{
-
char Title[MAXTITLE];
-
};
-
in which case main should be -
int main()
-
{
-
Book myBooks[MAXBOOKS];
-
int i = 0;
-
-
ifstream infile("library.txt");
-
-
for (i=0;i<MAXBOOKS; i++)
-
infile.getline(myBooks[i].Title, MAXTITLE);
-
return 0;
-
}
-
jjh 21
New Member
#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,MAXLE NGTH);
//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.
Banfa 9,065
Recognized Expert Moderator Expert
This is the error -
string tempTitle;
-
infile.getline(tempTitle,MAXLENGTH);
-
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 -
Book Books[MAXBOOKS];
-
-
int fillArray()
-
{
-
ifstream infile("library.txt");
-
-
for(int i = 0; i < MAXBOOKS; i++)
-
{
-
infile.getline(Books[i].title, sizeof Books[i].title);
-
cout << Books[i].title;
-
-
for(int j = 0; j < MAXKEYWORDS; j++)
-
{
-
infile.getline(Books[i].keyWord[j], sizeof Books[i].keyWord[j]);
-
}
-
}
-
-
return 0;
-
}
-
duh, you're incrementing the struct array, not the .title lol. I'm such an idiot.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 . "\" into table addresses";
$result = mysql_query($SQL) or die(mysql_error());
The file is located in the same directory as my .PHP file.
How do I...
|
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");
exit(1);
}
|
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 be able to place the PHP file on the
server, and I guess you probably can't do that either. Talk about catch
22...
The only other way I can think...
|
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 programs that read
the group from option files, you can add the local-infile=1
option to that group. However, to keep this from causing problems...
|
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 is for the sake of security. What
does that mean ?
| |
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 pretty well except when a particular cmd doesn't apply to one of the switches listed in my infile. the script seems to get stuck in these instances and...
|
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 binary
mode.
according to me, notepad opens files and each byte of the file
read, it converts that byte from ascii to its correct character and...
|
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 MAXBOOKS
Why wont it work when I use...
infile.getline(book.title, MAXBOOKS);
|
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 Tubbs
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |