Jump to content
Fórum Script Brasil
  • 0

Escrever Variáveis int em um arquivo .txt C++ usando fprintf();


Question

Estou fazendo alguns testes de manipulação de arquivos .txt sou iniciante e não consigo escrever no arquivo .txt uma variavel do tipo int, tentei de várias formas mas nenhuma funcionou

#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;

int main() {
 
	FILE *file;
	file = fopen("filename.txt", "rt");
	
	char mystring[100];
	char name[100];
	char sentence [256];
	int n;
	int teste = 98;
	
	
	if(file == NULL){
		cout << "ERROR 404";
		cout << "não achamos o arquivo database.txt";
	}
	
		//pegando todas os caracteres do .txt e escrevendo-as
		while(fgets(mystring, 100, file) != NULL){
			puts(mystring);
		}
		
		//escrevendo alguma coisa
   		printf ("Enter sentence to append: ");
   		fgets (sentence,256,stdin);
   		file = fopen ("filename.txt","a");
   		fputs (sentence,file);
   		
   		//escrevendo seu nome
   		for (n=0 ; n<3 ; n++)
   		{
     		puts ("please, enter a name: ");
     		gets (name);
     		fprintf (file, "Name %d [%-10.10s]\n",n+1,name);
     		break;
   		}
   		
   		fprintf(file, "\nEAE POW \n");
   		fprintf(file, teste);
   		
}

queria escrever a variável "teste" do tipo int não na tela, mas no arquivo .txt pelo método fprintf();

 

Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      148907
    • Total Posts
      645002
×
×
  • Create New...