1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Rep Power

    Exclamation reading from file and storing the values in an array!! HELP PLEASE!! :O

    Hey everyone!

    So i have a simple program that's supposed to read up to 50 values from a .txt file, store the values in an array, and print the values to the user. However, when I run the program, it just outputs nothing to the user..just a blank space, i don't see any values.... i created the .txt file and saved it into "My Documents" on my computer so I'm pretty sure the program knows how to access it....maybe there's another mistake in my code that i'm not catching?

    If anyone would like to help, I would greatly appreciate your help!!
    Thank You

    And here's my code:

    /*Written by: Kalpana Chinnappan
    Date: January 17, 2013
    Homework 1
        #include <stdio.h>
        int main (void)
        int nums[50];   //up to 50 element int array
        FILE *fp1;      //file pointer
        int i;
        //******************  code starts here ***************
        for(i=0;i<50;i++)   //initialize array with 0
        i=0;        //clean up and initialize LCV
        if ((fp1=fopen("votes.txt","r"))==NULL)
        printf("votes.txt failed to open\n");
        return 1;
            while((fscanf(fp1,"%d",&nums[i]))!=EOF) //scanf and check EOF
                printf("nums[%d] is %d\n",i,nums[i]);
        return 0;
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Rep Power
    Originally Posted by kal123456
    i created the .txt file and saved it into "My Documents" on my computer so I'm pretty sure the program knows how to access it..
    Huh??? They really need to teach you kids some basic MS-DOS!

    Do you understand basic directory structure? MS-DOS v2 borrowed it from UNIX. There's a root directory which contains files and sub-directories, each of which can contain more files and more sub-directories, forming a directory tree. The visual "desktop" metaphor that everybody's so enamoured with nowadays is that of "folders", but those "folders" are actually directories. You can traverse that directory tree with the chdir command, "change directory" or CD for short. Every file is in one specific directory and can be accessed through its directory path, either the absolute directory path starting from the root directory (called \ in MS-DOS, or / in UNIX/Linux) or a relative directory path from the current working directory.

    Actually, MS-DOS and Windows also introduces the idea of volumes in which each disk drive and device is a separate volume with its own root directory; eg, the hard drive C:. Though in UNIX/Linux, the entire system has one single root directory and devices and extra disk drives (eg, the floppy or the CD-ROM) are mounted under the /mnt directory. But the main idea here is the current working directory! Every program starts running with a current working directory, which is normally where the program's executable resides. When you open a file without giving it a directory path, then that file is assumed to reside in the current working directory. If that file is not there, then it cannot be found by the program; you have to tell the program where to find that file!

    Find the directory that the program's executable is in and copy your file there and see whether it works then. Or better, use the function that returns the program's current working directory and display it so that you will know where to put that file so that your program can find it. That function should be GetCurrentDirectory; RTFM at http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx.

IMN logo majestic logo threadwatch logo seochat tools logo