Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading a text file into an array

Tags:

arrays

c

I'm really new to programming, and after thinking hard about this for a week for a summer project, I'd really appreciate some help!

I'm trying to read in a long text file, which is just a long string (NB: not an actual programming string) of letters, and then put each letter into its place in the grid (the aim of the program is ultimately to solve a wordsearch) so far I've come up with the program below, which doesn't seem to be producing a grid, but rather just reprints the text file, preceded by the following:

{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf510    
{\fonttbl\f0\fmodern\fcharset0 Courier;}
{\colortbl;\red255\green255\blue255;}
\paperw11905\paperh16837\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\deftab720
\pard\pardeftab720

\f0\fs24 \cf0 

The program that I've written is this:

#include <stdio.h>
#include <stdio.h> 
#include <stdlib.h> 
#include <math.h>
#include <stdbool.h>

int main()
{
    int i,j;
    char myarray[26][26],x;
    FILE *myfile;

    for (j=0; j<26; j++)                 //initialise array elements all to zero
    {
        for (i=0; i<26; i++)
        {
            myarray[i][j]=0;
        }
    }

    myfile=fopen("*redacted*","r");
    if (myfile!=NULL) //check file actually opened
    {
        for (i=0; i<26; i++)
        {
            for(j=0; j<26; j++)
            {
                fscanf(myfile,"%c",&x); //read the values in
                myarray[i][j]=x;
            }
        }
        // data is now in the array called myarray
        fclose(myfile);        
    }

    else
    {
        printf("File not found");
    }

    for(i=0;i<26;i++)
    {
        for(j=0;j<26;j++)
        {
            printf("%c",myarray[i][j]);
        }
    }

}

Thank you for any help you can offer

like image 612
user2591837 Avatar asked Jul 17 '13 14:07

user2591837


People also ask

How do I load a file into an array?

In Java, we can store the content of the file into an array either by reading the file using a scanner or bufferedReader or FileReader or by using readAllLines method.

How do I read a text file into a list in Python?

To read a file into a list in Python, use the file. read() function to return the entire content of the file as a string and then use the str. split() function to split a text file into a list.

How do I add files to an array in Java?

File> files = new ArrayList<>(); and add the files like: files. add(file);


1 Answers

Here's the beauty of C:

You can read the file in a single operation, and save yourself the looping:

Something like

fread(myArray, sizeof(myArray), myfile)

You should probably initialize the array to all zeros before you do this, though:

char myArray[26][26] = { 0 };

Or fill it with zeroes if you don't initialize it:

 memset(myArray, 0, sizeof(myArray));

Also, you might want to print a newline ("\n") at the end of each outer loop in your printing section: otherwise the file contents will appear as one long, continuous string.

like image 107
Curt Avatar answered Sep 24 '22 17:09

Curt