C portability on Linux

Asked

Viewed 115 times

1

I’m starting to use the Compile and there they use Linux. I’m having some problems such as printing a text file. Check my program, it works on windows:

#include<stdio.h>
/*
escreva um programa que leia o nome de 10 pessoas e armaene esses nomes no arquivo de texto nomes.txt
*/

int main()
{
FILE *arquivo;
int i;
char nome[40], c;

arquivo=fopen("nomes.txt", "a");

printf("digite os 10 nomes:\n");

for(i=0; i<10; i++)
{
    gets(nome);
    fprintf(arquivo,"%s \n",nome);
}

fclose(arquivo);

arquivo=fopen("nomes.txt", "r+");

while((c=getc(arquivo))!=EOF)
    printf("%c",c);

return 0;
}

What’s different between writing on Linux and Windows so I can start using Fedora.

  • What’s the problem? Do you know if Compiler supports file writing? These sites do not usually give access to IO except the console that is redirected to the web page. The problem may be this.

  • "The biggest trick C has ever done is to convince the world that he doesn’t have a running time library". "The Greatest Trick that C Ever Pulled was convince the world that it does not have a Runtime". http://lucumr.pocoo.org/2013/8/18/beautiful-native-libraries/

  • what I want to know is this arquivo=fopen("nomes.txt", "r+");&#xA;&#xA;while((c=getc(arquivo))!=EOF)&#xA; printf("%c",c); is to read in windows in lunux is like?

  • 2

    Except that you should NEVER wear gets, I see no error in your code and it should work normally on Linux and Windows. If you don’t post what error you’re getting, there’s no way we can help you anymore. I used Gentoo, I tested the code exactly as you wrote it and it worked.

1 answer

2


Well, I ran your code on Ubuntu and compiled it good... just appeared the message:

test. c:18:5: Warning: ? gets' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations] gets (name); /tmp/ccQhpYWR. o: in function main': teste.c:(.text+0x45): aviso: thegets' Function is Dangerous and should not be used.

but it’s running... so I took a look around here this error is that this gets function is already outdated; the compiler runs but gives this warning there... I tested here in place of this gets the fgets(name, 100, stdin)... it worked out good.

  • thanks i know gets is outdated but the teacher asks so hahaha but already solved this problem ai =) thanks a lot

Browser other questions tagged

You are not signed in. Login or sign up in order to post.