Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,416 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,406 people online right now. Registration is fast and FREE... Join Now!




fscanf anf fgetc

 
Reply to this topicStart new topic

fscanf anf fgetc

snowg
15 Oct, 2008 - 07:08 AM
Post #1

New D.I.C Head
*

Joined: 15 Oct, 2008
Posts: 10

I have a .txt file that has the following infomation
1 "Pink" "So What"
2 "T.I." "Whatever You Like"
3 "Rihanna" "Disturbia"
tab key between the number and the string, tab key between the two strings
I have to read the file and store the file data in a table struct. My code does not do that correctly (I also have to ignore the ")
Can anybody help me?
CODE

#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
#define ARXEIO "test.txt"
   struct list
   {
      int number;
      long singer[80];
      long song[800];
   };
   struct list A[20];

   int i=0;
int  c;
int main(int argc, char *argv[])
{
    FILE *fp;  char ch; int count=0;

    if ((fp=fopen(ARXEIO, "r"))==NULL)

    {
       printf ("error in opening file %s", ARXEIO); exit(1);
    }
      else
    {
    printf("File opened successfully.\n");
}
for (i=0;i<20;i++)
{
fscanf(fp,"%d",&A[i].number);
while ((ch=fgetc(fp))==('\t'|'"'));
fscanf(fp,"%s",&A[i].singer);
while ((ch=fgetc(fp))==('\t'|'"'));
fscanf(fp,"%s",&A[i].song);
while ((ch=fgetc(fp))==('"'|'\n'));
printf("%d %s %s %d\n",A[i].number,A[i].singer,A[i].song,i);
}
while (getchar() !='e');
}

User is offlineProfile CardPM
+Quote Post

bodom658
RE: Fscanf Anf Fgetc
15 Oct, 2008 - 08:07 AM
Post #2

D.I.C Head
**

Joined: 22 Feb, 2008
Posts: 152



Thanked: 5 times
My Contributions
One thing I would try is reading in the strings as they are and then creating an external function to remove the "'s.

Also, try declaring less space for your artist name and song name, and the number of songs in the list, as this will use less memory.

Lastly, I would not use a for loop when putting information into the array, as this will cause you to run out of data but still be trying to put data into structures... try a while loop! Do the same for writing it to the screen, only write things as long as there is actually something to write!

Something along the lines of while(array[i] > 0)

Hope I've Helped!
Happy Coding!

~Bodom
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:50PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month