I need to make a program in which the user chooses a continent. The program will read the file of the chosen continent. Then, the program will ask the capital of a country of the chosen continent. Right now, I'm focused on Europe. (The file is in Spanish, but is fine)
The first line is the Country and the second line the capital and so on. I'm not very good at this. Surely it's a stupid mistake or I have misunderstood the theory. The code is executed but reads the file in a strange way. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#define N 33
int Europa(char EuropaP[N][N], char EuropaC[N][N]);
//int Asia();
FILE *eu;
FILE *as;
int main()
{
int p1, salir;
char EuropaP[N][N];
char EuropaC[N][N];
while(salir != 0)
{
printf("-----MENU PRINCIPAL-----\n"); //main menu where the users selects the continent.
printf("Elige un continente\n\n");
printf("1. Europa\n");
printf("2. Asia\n");
printf("\nEscribe 0 para salir");
printf("\nSeleccion: ");
scanf("%d", &p1);
if (p1 == 1)
{
Europa(EuropaP, EuropaC);
}
else if (p1 == 2)
{
//Asia();
}
else
fclose(eu);
//fclose(as);
break;
}
return 0;
}
int Europa(char EuropaP[N][N], char EuropaC[N][N])
{
int vida, c, i;
eu = fopen("Europa.txt", "r");
if (eu == NULL){
printf("No se ha abierto correctamente el fichero");
}
else
for(i=0; i<33; i++)
{
fscanf(eu, "%s\n%s", EuropaP[i], EuropaC[i]);
}
for(c=0; c<33; c++)
{
printf("%s\n%s", EuropaP[c], EuropaC[c]);
printf("\n");
}
}
Example of execution:
As you can see the file is not being read correctly. I don't know how to copy the countries and capitals to the array.
PROBLEM FIXED! I added return 0; over here:
if (eu == NULL){
printf("No se ha abierto correctamente el fichero");
return 0;
}
changed the directory of the .txt file:
int Europa(char EuropaP[N][N], char EuropaC[N][N])
{
int vida, c, i;
eu = fopen("/Users/****/Desktop/Europa.txt", "r");
and finally, I'm using fgets instead of fscanf.
else
for(i=0; i<N; i++)
{
fgets(EuropaP[i], N, eu);
fgets(EuropaC[i], N, eu);
}
for(c=0; c<N; c++)
printf("%s\n%s", EuropaP[c], EuropaC[c]);
printf("\n");
For lines with spaces, fgets
would work. The newline can be removed using strcspn
.
int Europa(char EuropaP[N][N], char EuropaC[N][N])
{
int vida, c, i;
eu = fopen("Europa.txt", "r");
if (eu == NULL){
printf("No se ha abierto correctamente el fichero");
return 0;
}
for(i=0; i<33; i++)
{
if ( ! fgets ( EuropaP[i], N, eu)) {
printf ( "fgets EOF\n");
return 0;
}
EuropaP[i][strcspn ( EuropaP[i], "\r\n")] = 0;//remove newline
if ( ! fgets ( EuropaC[i], N, eu)) {
printf ( "fgets EOF\n");
return 0;
}
EuropaC[i][strcspn ( EuropaC[i], "\r\n")] = 0;//remove newline
printf("%s\n%s\n", EuropaP[i], EuropaC[i]);
}
return 1;
}
Iterate through the string and when a newline or carriage return is found, set a zero to terminate the string.
int Europa(char EuropaP[N][N], char EuropaC[N][N])
{
int vida, c, i;
int each = 0;
eu = fopen("Europa.txt", "r");
if (eu == NULL){
printf("No se ha abierto correctamente el fichero");
return 0;
}
for(i=0; i<33; i++)
{
if ( ! fgets ( EuropaP[i], N, eu)) {
printf ( "fgets EOF\n");
return 0;
}
each = 0;
while ( EuropaP[i][each]) {
if ( '\r' == EuropaP[i][each] || '\n' == EuropaP[i][each]) {
EuropaP[i][each] = 0;
break;
}
each++;
}
if ( ! fgets ( EuropaC[i], N, eu)) {
printf ( "fgets EOF\n");
return 0;
}
each = 0;
while ( EuropaC[i][each]) {
if ( '\r' == EuropaC[i][each] || '\n' == EuropaC[i][each]) {
EuropaC[i][each] = 0;
break;
}
each++;
}
printf("%s\n%s\n", EuropaP[i], EuropaC[i]);
}
return 1;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.