arrays

This is a discussion on arrays within the C++ Programming forums, part of the General Programming Boards category; OK, Is there ANY way to write to /read from a 2-dimensional Array to a text file?? Here's what i ...

  1. #1
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31

    Question arrays

    OK,
    Is there ANY way to write to /read from a 2-dimensional Array to a text file??

    Here's what i got so far:

    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>

    main()
    {
    FILE*wrt;
    int a,b,w,x,y,z;

    cin>>w>>x>>y>>z;
    int grid[2][2]={{w,x}, {y,z}};
    for (int i=0; i<2; i++){
    for (int I=0; I<2; I++){
    wrt=fopen("d:\\cpp\\q", "w");
    a=grid[i][I];
    fputw(a, wrt);
    fclose(wrt);
    wrt=fopen("d:\\cpp\\q", "r+");
    cout<<fgetw(wrt);
    }
    }

    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    The basic flow of your program should be:

    declare variables
    input data into variables
    declare file pointer
    open file for writing
    use loop(s) to write data to file
    open file for reading
    use loop(s) for reading file data into array
    close file


    if you open the file each time through the loop(s) you will probably overwrite the data you previously entered unless you use a different flag. Just open it once for writing and then once for reading. Then close the file.

    you really don't need the variable a at all, just use the array indicies to indicate which specific int you are working with.

    fputw() doesn't exist, that I remember. fprintf(), fscanf(), fread(), fwrite(), fputc(), fputs() I remember, but not fputw().

    It's been quite a while since I used file pointers, but I pretty sure the general theme of my comments still holds even if the details are off somewhat.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21