|C| Ska läsa ord från en .txt fil och sen lägga det i en array.
Tjena sweclockers.
Har ett problem med att läsa text från en .txt fil och sedan placera orden i en array.
min kod ser ut såhär
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fstream>
#include <iostream>
#include <string>
#include <conio.h> /* for clrscr */
#include <dos.h> /* for delay */
#include <math.h>
using namespace std;
#define MAX_WORD_LEN 20
const int MAX_GUESS = 6;
const char ABORT_CH = '0';
const int LOOSE = 0;
const int WIN = 1;
const int ABORTED = 2;
FILE *fr; /* declare the file pointer */
/* -------------- Funktionsdeklarationer -----------------*/
/* IO */
typedef struct post
{
char ord[MAX_WORD_LEN];
} POSTTYP;
void from_file_to_array( );
char get_user_input(char *str);
/* Logic */
/* File IO */
/* Utilities */
void clear_stdin();
/************************************************************
*
* MAIN
*
*
************************************************************/
int main()
{
int a = 0;
POSTTYP allwords[200]; /* array where you lay all of the words from "words.txt" */
char line[80];
char word[MAX_WORD_LEN]; /* Word to guess (from file) */
char mask[MAX_WORD_LEN]; /* Masked word (shown on screen) */
int i = 0;
int ch; /* User selection */
int count; /* Number of guesses */
int result; /* Winner */
//int len;
/* -------------------- Programstart -----------------*/
srand( time(0) );
while( true )
{
/*
* Init section
*/
from_file_to_array(allwords);
printf( "Valkommen till Alexander Ekstrands HANG MAN 2012\n");
printf( "Du har %d gissningar på dig (avbryt med 0)\n", MAX_GUESS );
//printf( "Datorn har nu valt ett ord på %d bokstaver\n", len );
while( true )
{
/* GAME MAIN LOOP */
}
if( result == WIN )
{
printf("Grattis du vann!\n");
}
else if( result == LOOSE )
{
printf("Tyvarr du forlorade ordet var: %s\n", word);
}
else
{
printf("Avbrutet\n");
}
ch = get_user_input("Vill du spelas igen (j/n)?): ");
if( ch == 'n' || ch == 'N' )
{
break;
}
}
return 0;
}
/***********************************************************
*
* --------- Funktionsdefinitioner ----------------
*
***********************************************************/
char get_user_input(char *str){
char tkn= '0';
return tkn;
}
void from_file_to_array(POSTTYP allwords[])
{
int a = 0;
POSTTYP temp[MAX_WORD_LEN];
char line[200];
fr = fopen ("words.txt", "rt"); // open the file for reading
// elapsed.dta is the name of the file
// "rt" means open the file for reading text
while(fgets(line, 80, fr) != NULL) // get a line, up to 80 chars from fr. done if NULL
{
sscanf (line, "%s", temp[a].ord); // convert the string to a long int
allwords[a] = temp[a];
printf ("%s\n", temp[a].ord);
a++;
}
fclose(fr); // close the file prior to exiting the routine
}