WSQ09

--Originally published at Victor´s Spot

This task was very interesting and kind of hard but I believe I developed some habilities that will help me in my future, because my career requires having good knowledge and it is great to know and use these skills on my profesional life.

It consisted in “So for this assignment I would like to see you create a function that receives as parameter the name of a file (this would be a string value like data.txt) and your function counts the number of lines and the number of characters in the file which it returns as a single value (but with two values). You will want to look at how to create/define and return a struct value from a function and how to open and read text files line by line” (Bauer, 2017)

WSQ09

I believe I made used of these mastery topics, taken from kenscourses.com:

  1. Use of loops with “while” and “do while”
  2. Creation and use of strings

And now a little poem:

You can count a char

but you can not be a star

So you tried to be who you are

And succeed in life

You can download my code here: https://gist.github.com/victorcmadrigal/b2b3b3b74f01c37a05f1c1dbbe08edad

 


WSQ09

--Originally published at PZ

This week assignment was much more easy than the wsq 8, but I think that this was more interesting and mean a very big step bacause now Im working with a text file that is not in the .cpp file.

First and the most important was to learn de library fstream that is required to read a file of text, I watched this video that help me a lot https://www.youtube.com/watch?v=xZh0oRKIPCw

After that I searche on how to count characters and lines, and for that I used the library string, that we have already seen, and getline(). I used this https://www.youtube.com/watch?v=5xEkuvTxQ4Y&index=46&t=524s&list=WL video as reference and also a blog from my friend Esteban who helped me a lot https://blogdeesteban.000webhostapp.com/.

Codigo

#include <iostream>
#include <string>
#include <fstream>

 

using namespace std;

int main() {

int car=0,lines=0; //variables para contar caracteres y lineas
ifstream ar; // definir la variable de texto

string input;
ar.open(“prueba.txt”,ios::in); //abrir el archivo prueba

while (!ar.eof()) {
getline(ar,input,’\n’); //del archivo ar guardad como input lo que hay ahi hasta que se de enter
car +=input.size();
lines++;
}
cout<<“El numero de caracteres es :”<<car<<endl;
cout<<“El numero de renglones es :”<<lines<<endl;

 

 

return 0;

 

}


WSQ09 – Data and Files

--Originally published at BRENDA

For this task, I covered the topics of strings. I also decided to learn how to do a structure, which is a way to create a function that returns two values instead of just one.

I had to include this two libraries:

#include <fstream>
#include <string>

The way I wrote my code so that it could read a file is as following:

struct structure counttext (string name) {
fstream file;
file.open(name.c_str());

I used (name.c_str()) to be able to read the string name which is in fact an array of characters.

A structure is done this way:

  1. Define the name of the structure and the values it will return:
    struct structure {
    int chars, lines;
    };
  2. Create a function with the structure:
    struct structure counttext (string name) {
    }
  3. Define the two values it will return with the function created:
    struct structure answer;
    answer.chars = countchars;
    answer.lines= countlines;
  4. Use the function combined with the structure inside main this way:
    struct structure answer = counttext(file);
  5. Return the value to the user:
    cout << answer.lines << answer.chars;

I also used the function getline(file, line) & line.length() to read the lines in the file and to calculate the length of the line in characters, respectively.

Here is my full code:

https://github.com/brendaruizt/TC1017/blob/master/multipart.cpp

multipart

 


Datos y archivos de partes múltiples

--Originally published at RON

Para esta práctica necesite leer un poco algunas páginas, ademas gracias a la ayuda de mi profesor Ken y a la aportación del blog de Alexa, logre realizar este WSQ. Realmente no es muy largo ni difícil pero ere un tema nuevo. Les dejo mi código y fuentes consultadas en la parte de abajo. WSQ09

Fuentes:

http://www.forosdelweb.com/f96/como-contar-lineas-archivos-1026619/

https://amixeblog.wordpress.com/2017/10/21/wsq-09-multipart-data-and-files/comment-page-1/#comment-10

https://gehrcke.de/2011/06/reading-files-in-c-using-ifstream-dealing-correctly-with-badbit-failbit-eofbit-and-perror/

Archivo .cpp:

https://drive.google.com/a/itesm.mx/file/d/0BwFGq6oJuVvVTzRCcnJRRjdUS00/view?usp=sharing


Homework 09

--Originally published at Blog de Esteban

Este programa es sencillo, lo único que tienes que hacer es dedicarle tiempo a leer sobre la libreria de fstream, yo tarde un poco porque quería entender muy bien como funciona, pero la verdad es que lo puedes hacer en unos cuantos minutos.

quizá lo que mas se puede llegar a complicar es contar los renglones, aquí se los explico:

“getline” tiene 3 parámetros que se ponen dentro de un paréntesis separados por comas.

el primero que deberás poner es el de la variable ifstream, después el string donde quieres guardar los caracteres del documento y por ultimo la condición, esta no siempre se pone, pero en este caso si, ya que queremos contar saltos de linea, esto en c++ se representa con \n.

te quedaría algo así:

getline(doc,str,\n);

*esto tomando en cuenta que declaraste doc como ifstream y str como string.

puedes descargar mi código explicada en el siguiente link: https://www.dropbox.com/s/9pbmq6ai50gpqtg/Tarea9.cpp?dl=0

También te pongo a continuación 2 videos que me ayudo mucho a mi para aprender estos temas: