hi guys i have an urgent problem....we have been asked to make a battleship game in C which will have the formation of ships taken by a file txt.....for example 00000000
ppp00000
0000nnnn
00000000
etc...i want to open the file txt and then put every single character 0,p,n etc to an array,,,for example array[0]=p,array[1]=p array[3]=0...
can u tell me how?i found only the down code which though cant change line....and if i put many chars it has error....pliz help emergency,,,

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{ 
  system("chcp 1253 > nul");  
  char buffer[6] = {0};  /* initialized to zeroes */
  int i, rc;
  FILE *fp = fopen("c:\\1.txt", "rb");
  if (fp == NULL) {
    perror("Failed to open file \"myfile\"");
    return EXIT_FAILURE;
  }
  for (i = 0; (rc = getc(fp)) != EOF && i < 6; buffer[i++] = rc)
    ;
  fclose(fp);
  if (i == 6) {
    puts("The bytes read were...");
    printf("%c %c %c %c %c %c \n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4],buffer[5]);
  } else
    fputs("There was an error reading the file.\n", stderr);
  system("PAUSE");
  
}

Recommended Answers

All 10 Replies

How many characters per row (and how many rows)?
Is that fixed, or are you supposed to work it out from reading the file?

Consider char map[4][8]; as being just enough for holding the 4-lines of map you posted.

well it should be 10X10 the problem is how can i put each and every char to the array since the txt file is 10x10...

umm, so then what's wrong with char map[10][10]; ?

Ever hear of nested for loops?

You know, a loop for rows, which contains a loop for columns ?

An outline in pseudo code:

char map[10][10]
char myString[12]

for(each row) {
  
  fgets(myString, 10, filePointer);
  k = 0
  for(each column in the row) {
     map[row][col] = myString[k++]
  }  
}
commented: sorry man, that's just sloppy. -1

adak

if youre gonna post pseudo code, then dont structure it as C, and box it as C-syntax text. that just confuses the hell out of some new people.

also, why would you increment 'k' when you have the same variable indexed as columns in the for loop?

finally, your use of fgets is broken, as it won't read a 10 character line like you think it will.

so now that you've made a big mess, let's just give this piece to him so it makes some sense.


mariosbikos

here you go, mate. start with this.

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

#define MAX_ROWS       10
#define MAX_COLS       10

int main(void)
{
    FILE *fileptr;

    char map[MAX_ROWS][MAX_COLS];
    char fileRow[MAX_COLS + 2];          // account for newline and NULL

    int row, col;

    if ((fileptr = fopen("battleship.txt", "r")) == NULL)
    {
        perror("File Open Error");
        return -1;
    }

    for (row = 0; row < MAX_ROWS; row++)
    {
        fgets(fileRow, sizeof(fileRow), fileptr);

        for (col = 0; col < MAX_COLS; col++)
            map[row][col] = fileRow[col];
    }

    fclose(fileptr);

    // now you have the map loaded, 
    // so do the rest of your program

    return 0;

}

NOTE: this requires that your "battleship.txt" file (or whatever you want to call it) is precisely the size of your intended map, that each line has one and only one newline after each row of characters, and that there is absolutely no additional whitespace anywhere in the file.

variation from this will cause the program to fail in variety of ways, as there is no error checking included in the file read loop.

any program that you submit should have some amount of error checking.


.

It was just some pseudo code to get him started on some idea's.

Must have been a fair idea, because your code uses them. ;)

It was just some pseudo code to get him started on some idea's.

Must have been a fair idea, because your code uses them. ;)

Sure, its one idea among many.

Personally, I wouldn't have done it that way, i would have used fgetc() to check each element, avoid whitespace, and parse on the newlines. Then i could have avoided the whole disclaimer about the file read method being extremely fragile and breaking if you looked at it cross-eyed. But whatever.

Yeah, i just stuck to just fixing your mess, so he could see why it was broken. it has value as a fundamental lesson of what not to do.


.

He expressed no problem with the idea's I posted.

Messy marshes support far more life, than beautiful pristine forests.

Idea's need to be messy, sometimes.

guys i love u u found it thnxxxxxxx this site is very good and so are u thnxxxx

commented: love you too! :) +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.