473,399 Members | 2,774 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,399 software developers and data experts.

Reading data into struct

Hello all,

I have an assignment where I have to take a text file of music albums and read them into a struct, then alphabetize the songs within each album and then alphabetize the albums. My problem is getting the data read into the struct. I think if I can pass that hurdle, I can do the sorting. Here is the structure of the input file.

NAME OF ALBUM #1
Name of Singer #1
Name of Singer #2
Name of Singer #3

1. Song #1
2. Song #2
3. Song #3

====================
NAME OF ALBUM #2

...

The name of the album is in all caps, the artists are one per line, the songs each have a number in front of them and each album is separated by a row of equal signs.

Now, here are the limitations.

We have not covered vectors yet (don't ask me why) so I'm confined to using arrays and structs. We have not covered regular expressions, and so I have ABSOLUTELY no clue how to check for things like "if a line is all caps then read it into album[0].title". Furthermore I don't know how to increment my loop counter so that everytime I hit a line of equal signs I read the next block of lines into my next struct.

I understand you aren't here to do my homework for me. Please help me at least get data read into the struct.

Thanks.
Mar 21 '07 #1
1 2386
Roonie
99
well, i think you should start by writing a loop that will take every input line in succession.

once you have an input line in a char[], strstr() will search for substrings in your input.

example:
Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3.   char example[] ="This is a simple string";
  4.   char * result;
  5.   result = strstr (example,"simple");
  6.   cout << result;
  7.   system("PAUSE");
  8.   return 0;
  9.  
  10. //this will output "simple string"
  11. }
you can use the strstr() as the conditional for an if statement. it will return 1 if the match succeedes . . . so

Expand|Select|Wrap|Line Numbers
  1. if(strstr(example,"========"))
  2. {
  3. <next record>
  4. }
you can search for lower case letters to know that its is an artist's name and you will know that the first entry is the album name. any line after the first will be an artist, and then after a blank line, are song names.
Mar 21 '07 #2

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

Similar topics

20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
9
by: Alex Buell | last post by:
I have a small text file which consist of the following data: ]] And the code I've written is as follows: ]] The trouble is, I can't work out why it goes into an infinite loop reading the...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
4
by: Mastastealth | last post by:
I'm trying to create a program to read a certain binary format. I have the format's spec which goes something like: First 6 bytes: String Next 4 bytes: 3 digit number and a blank byte --- Next...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
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,...
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.