Thread: Printing a contents of a file

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    2

    Printing a contents of a file

    i have been working on this code that is supposed to read contents of a file, sort them into different dadtamembers of a struct and print the results, for some reason when i compile it i just end up getting a bunch of zeros.

    Heres the code (ignore the commented out parts):
    Code:
    //
    //  main.c
    //  Labexam2
    //
    //  Created by Blair Robinson on 4/20/17.
    //  Copyright © 2017 Blair Robinson. All rights reserved.
    //
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct song_t{
        char title[100];
        char artist[100];
        char album[100];
        int year;
        int duration;
    }song;
    
    void printSong(song *s){
        int numMin,numSec;
        numMin=s->duration/60;
        numSec=s->duration%60;
        if (numSec/10>=1){
        printf("%s\n%s\n%s\n%d\n%d:%d\n\n",s->title,s->artist,s->album,s->year,numMin,numSec);
        }
        else {
            printf("%s\n%s\n%s\n%d\n%d:0%d\n\n",s->title,s->artist,s->album,s->year,numMin,numSec);
        }
    }
    
    song readSong(FILE *inp,song songPtr[],int numSongs){
        inp=fopen("songs.txt","r");
        int i,j=0;
        char songDataC[100];
        char songDataI[100];
        
        
                while (!feof(inp)){
                    for(i=1;i<6;i++){
                    fgets(songDataC,100,inp);
                    switch(i){
                        case 'A':
                            i=1;
                            strcpy(songPtr[j].title,songDataC);
                            break;
                        case 'B':
                            i=2;
                            strcpy(songPtr[j].artist,songDataC);
                            break;
                        case 'C':
                            i=3;
                            strcpy(songPtr[j].album,songDataC);
                            break;
                        case 'D':
                            i=4;
                            strcpy(songDataI,songDataC);
                            songPtr[j].year=atoi(songDataI);
                            break;
                        case 'E':
                            i=5;
                            strcpy(songDataI,songDataC );
                            songPtr[j].duration=atoi(songDataI);
                            break;
                    }
                    j++;
                }
                    printSong(&songPtr[j]);
            }
           /* if (i==1){
                fgets(songDataC,100,inp);
                strcpy(songPtr[j].title,songDataC);
            }
            else if (i==2){
                fgets(songDataC,100,inp);
                strcpy(songPtr[j].artist,songDataC);
            }
            else if (i==3){
                fgets(songDataC,100,inp);
                strcpy(songPtr[j].album,songDataC);
            }
            else if (i==4){
                fgets(songDataI,100,inp);
                songPtr[j].year=atoi(songDataI);
                
            }
            else if (i==5){
                fgets(songDataI1,100,inp);
                songPtr[j].duration=atoi(songDataI1);
            }
            j++;
        }
            printSong(&songPtr[j]);
        } */
            return songPtr[j];
    
        }
    /*void no_songs(int duration,int j){
        printf("There are no songs as long or longer than %d seconds in the database",duration);
    }
    
    
    void searchSong(song song[],int size, int duration){
        int i=0,j=0;
        
        for(i=0;i<size;i++){
            if (song[i].duration>duration){
                printSong(&song[i]);
                j=1;
            }
            if (j!=1){
                no_songs(duration,j);
            }
        }
    } */
    
    int main(void) {
        song *songArray=NULL;
        int numSongs=0,i=0,songMin,songSec,totsec;
        FILE *inp;
        char playAgain='y';
        
        while (playAgain !='q'){
            songArray=(song*)malloc(numSongs*sizeof(song));
                songArray[i]=readSong(inp,songArray,numSongs);
                //printSong(songArray);
            
            
            printf("Enter the minutes of the desired song: ");
            scanf(" %d",&songMin);
            printf("Enter the seconds of the desired song: ");
            scanf(" %d",&songSec);
            
            totsec=songMin*60+songSec;
            
            //searchSong(songArray,numSongs,totsec);
            
            printf("Press any key to search again or q to quit: ");
            scanf(" %c",&playAgain);
        }
        
        printf("Goodbye!");
    
        return 0;
    }
    Attached Files Attached Files
    Last edited by Salem; 04-21-2017 at 01:02 AM. Reason: Inlined code for easy reading

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    What do you think this piece of code does?
    Code:
    for(i=1;i<6;i++){
        fgets(songDataC,100,inp);
        switch(i){
            case 'A':
                i=1;
                strcpy(songPtr[j].title,songDataC);
            break;
            case 'B':
                i=2;
                strcpy(songPtr[j].artist,songDataC);
            break;
            case 'C':
                i=3;
                strcpy(songPtr[j].album,songDataC);
            break;
            case 'D':
                i=4;
                strcpy(songDataI,songDataC);
                songPtr[j].year=atoi(songDataI);
            break;
            case 'E':
                i=5;
                strcpy(songDataI,songDataC );
                songPtr[j].duration=atoi(songDataI);
            break;
        }
    }
    Hint: 'A' isn't the same as 1

    You seem to don't know how switch statements work. You may want to look up some tutorials on that.
    Last edited by GReaper; 04-21-2017 at 01:03 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    2
    okay so i changed the switch statements to if and else if statements because I'm more familiar with those

    Code:
    //
    //  main.c
    //  Labexam2
    //
    //  Created by Blair Robinson on 4/20/17.
    //  Copyright © 2017 Blair Robinson. All rights reserved.
    //
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct song_t{
        char title[100];
        char artist[100];
        char album[100];
        int year;
        int duration;
    }song;
    
    void printSong(song *s){
        int numMin,numSec;
        numMin=s->duration/60;
        numSec=s->duration%60;
        if (numSec/10>=1){
        printf("%s\n%s\n%s\n%d\n%d:%d\n\n",s->title,s->artist,s->album,s->year,numMin,numSec);
        }
        else {
            printf("%s\n%s\n%s\n%d\n%d:0%d\n\n",s->title,s->artist,s->album,s->year,numMin,numSec);
        }
    }
    
    song readSong(FILE *inp,song songPtr[],int numSongs){
        inp=fopen("songs.txt","r");
        int i,j=0;
        char songDataC[100];
        char songDataI[100];
        
        
                while (!feof(inp)){
                    for(i=1;i<6;i++){
                    fgets(songDataC,100,inp);
                    switch(i){
                        case 'A':
                            i=1;
                            strcpy(songPtr[j].title,songDataC);
                            break;
                        case 'B':
                            i=2;
                            strcpy(songPtr[j].artist,songDataC);
                            break;
                        case 'C':
                            i=3;
                            strcpy(songPtr[j].album,songDataC);
                            break;
                        case 'D':
                            i=4;
                            strcpy(songDataI,songDataC);
                            songPtr[j].year=atoi(songDataI);
                            break;
                        case 'E':
                            i=5;
                            strcpy(songDataI,songDataC );
                            songPtr[j].duration=atoi(songDataI);
                            break;
                    }
                    j++;
                }
                    printSong(&songPtr[j]);
            }
           /* if (i==1){
                fgets(songDataC,100,inp);
                strcpy(songPtr[j].title,songDataC);
            }
            else if (i==2){
                fgets(songDataC,100,inp);
                strcpy(songPtr[j].artist,songDataC);
            }
            else if (i==3){
                fgets(songDataC,100,inp);
                strcpy(songPtr[j].album,songDataC);
            }
            else if (i==4){
                fgets(songDataI,100,inp);
                songPtr[j].year=atoi(songDataI);
                
            }
            else if (i==5){
                fgets(songDataI1,100,inp);
                songPtr[j].duration=atoi(songDataI1);
            }
            j++;
        }
            printSong(&songPtr[j]);
        } */
            return songPtr[j];
    
        }
    /*void no_songs(int duration,int j){
        printf("There are no songs as long or longer than %d seconds in the database",duration);
    }
    
    
    void searchSong(song song[],int size, int duration){
        int i=0,j=0;
        
        for(i=0;i<size;i++){
            if (song[i].duration>duration){
                printSong(&song[i]);
                j=1;
            }
            if (j!=1){
                no_songs(duration,j);
            }
        }
    } */
    
    int main(void) {
        song *songArray=NULL;
        int numSongs=0,i=0,songMin,songSec,totsec;
        FILE *inp;
        char playAgain='y';
        
        while (playAgain !='q'){
            songArray=(song*)malloc(numSongs*sizeof(song));
                songArray[i]=readSong(inp,songArray,numSongs);
                //printSong(songArray);
            
            
            printf("Enter the minutes of the desired song: ");
            scanf(" %d",&songMin);
            printf("Enter the seconds of the desired song: ");
            scanf(" %d",&songSec);
            
            totsec=songMin*60+songSec;
            
            //searchSong(songArray,numSongs,totsec);
            
            printf("Press any key to search again or q to quit: ");
            scanf(" %c",&playAgain);
        }
        
        printf("Goodbye!");
    
        return 0;
    }
    Attached Files Attached Files
    Last edited by Salem; 04-24-2017 at 12:44 AM. Reason: Inlined (again!)

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Your new attached file and the old one are 100% the same...
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-12-2013, 08:04 PM
  2. Printing contents of a text file
    By never_lose in forum C Programming
    Replies: 10
    Last Post: 04-28-2011, 09:25 AM
  3. File I/O..Reading a file and printing contents
    By eeengenious in forum C Programming
    Replies: 2
    Last Post: 03-14-2011, 05:58 PM
  4. Printing contents of an input file to the screen
    By babe20042004 in forum C++ Programming
    Replies: 2
    Last Post: 11-24-2009, 12:57 PM
  5. Printing out contents of a file to the screen
    By Simon in forum C Programming
    Replies: 18
    Last Post: 10-21-2002, 08:05 AM

Tags for this Thread