Show and modify files

0
#include <stdio.h>
#include <stdlib.h>

    FILE *fd;

    int main()

    {
        int c;

    char direccion[] = "C:\Users\ferneicito\Desktop\Programa\hola.txt";

        fd = fopen(direccion, "a+");
        if (fd == NULL)
        {
            printf ("Error al crear archivo");
            return 1;
        }

        while ((c=getchar())!= EOF)
        {
            fputc(c,fd);
        }
        fclose (fd);
        return 0;
    }

Hey guys, someone could help me, it's a problem with the archives issue and what I need is that when I compile add text, and ready to close it, when compiling again what I wrote before and continue writing, I I do but I can only add text and when closing and reopening I do not get the above written but in the .txt file if I get it, what could I do there? Thanks

    
asked by Jhon 07.12.2017 в 20:51
source

1 answer

-1

As far as I could see in your request, you search that when you run your program, add text, close the program and reopen it to show you what you have entered.

But in your code you do not read the content that stores the file, and if you do not do it you will not be able to read it.

I do not know much about the C language but I understand and I use C ++ very well, I know they are different things, but with a little research and based on my knowledge, I created a code, where I think that is what you seek to achieve. And I also think that if you do not understand the c ++ language with a little research in the files or files area you will be able to understand it.

Anyway I tried to comment the code in a comprehensible way so that you understand it.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
   char Texto[100];
   char AgregarMasTexto;
   ofstream Archivo_Escritura;
   Archivo_Escritura.open("hola.txt", ios::out | ios::app);

    if(Archivo_Escritura.is_open())
    {
       cout << "\tEscritura del archivo." << endl << endl << endl << endl;
       cout << "Desea agregar texto a su archivo (S/N): ";
       cin >> AgregarMasTexto;

        while((AgregarMasTexto == 's') || (AgregarMasTexto == 'S'))
        {
           cout << endl << endl << endl << "Ingrese el texto que desee: ";
           cin >> Texto;
           Archivo_Escritura << Texto << endl;
           cout << "Desea agregar mas texto(S/N): ";
           cin >> AgregarMasTexto;
        }
    }

    else
    {
       cout << "El archivo no fue creado correctamente." << endl;
    }

    ifstream Archivo_Lectura;
    Archivo_Lectura.open("hola.txt", ios::in); 

    if(Archivo_Lectura.is_open())
    {
      cout << endl << endl << endl << endl << endl;
      cout << "____________________________________________." << endl << endl;
      cout << "\tLectura del archivo." << endl << endl << endl << endl;
      Archivo_Lectura >> Texto;
      cout << "El texto que existe en su archivo es el siguiente: "<<endl<<endl;

      while(!Archivo_Lectura.eof())
      {
          cout << Texto << endl;
          Archivo_Lectura >> Texto;
      }
     cout << "______________________________________________." << endl << endl;
   }

   else
   {
      cout << "El archivo no fue creado correctamente." << endl;
   }

   Archivo_Lectura.close();
   Archivo_Escritura.close();
   return 0;
}
    
answered by 08.12.2017 в 02:51