in c given this structure how would you read a file into it
in c++ given this structure how would you read a file into it if the file is written in order of english word, french word, english word, french word ect.
constant int MAX =50;
struct dictionary
{ char french [30];
char English [30]; };
lets say the file is named words, and each word has a limit of 30 char. word_length 30
thank you for your time
Solution
#include <iostream>
#include <vector>
#include <string.h>
#include <fstream>
using namespace std;
struct dictionary{
char french [30];
char english [30];
};
int main(){
string fileName = \"words.txt\"; //reading the file named words.txt
// Open file
ifstream infile;
infile.open(fileName); //opening file
vector<dictionary> words; //declaring a vector to store the words
int n = 0; //counter to know if to add the word to french or english
char frenchWord[30]; //auxillary variable
char englishWord[30]; //auxillary variable
while (!infile.eof()) {
if(n%2==0){ //add to english if n even
infile>>englishWord;
}
else if(n%2==1){ //add to french if n odd
infile>>frenchWord;
}
dictionary word;
strcpy(word.french, frenchWord); //copy the word
strcpy(word.english, englishWord); //copy the word
words.push_back(word); //add word to words vector
n++;
}
infile.close(); //close file
}
