Thread: Storing integers from file into arrays???? please help

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    11

    Unhappy Storing integers from file into arrays???? please help

    1.) i need to read a file containing 32 bit numbers and store them into an array of integers( an array of 32 bit numbers)

    **how would i read the file to get it into arrays, and not just one lots of them of 32 bit numbers**

    2)i must also modify my program so that an integer is used to index through the array and print its contents in sequence to the screen.

    3) modify the index to start prointing from anywhere in the array

    if anyone has any ideas or knows how to do the following step please help me..

    thanks guys

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I would start by looking at the 'ifstream' class. Google it and see what you come up with.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    1) Hint: use a while() loop. How you read them depends on what the file looks like. Is it a text file?

    2) Hint: use a for() loop. You could also use a while() loop.

    3) Same as 2) but ask the user for a starting index, and start the loop at that index.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    11
    yes its a text file containing the 32 bit numbers... i understand that i have to use while loops and for loops but im not sure on how to get it started...

    the file contains 32 bit numbers each on a seperate line

    then i have to store each 32 bit number (each line )into its own array

    so i got no clue on how to actually get every 32 bit number into different array...??? wouldnt you have to declare a seperate array for every 32 number and when reading the file eg

    in = fopen("numbs.txt","r");

    fscanf(in,%d,&numbers);

    whould i put something like &numbers[ ] instead of &numbers would that work???

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >in = fopen("numbs.txt","r");
    If you are using C++, use should really use ifstream:

    ifstream in("numbs.txt");

    You probably need to read up on arrays. Here's how you access an array element:
    numbers[2] for example will access the 3rd element (arrays start at index 0).

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    ifstream is really easier to use. But if you insist:

    fscanf(in,%d,&numbers[0]);

    This would read an int into the first location of the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. integers storing as symbols in arrays
    By rjcarmo in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2003, 01:17 AM