Multipart Data and Files

--Originally published at my programming blog

The WSQ of last week was to make a program that could read a file and return how many lines and how many characters does it have.

This links were very useful for the use of structs, how to count the lines in a file and to count how many characters .

So what I did was:

  1. I included the fstream library ,so that I can read the file, and the string library so that I can use the getline function.
  2. Then I created my struct, I named it Results and I declared my two integers that my function will return at the end, which are sum (the number of characters) and number_lines (number of lines).
  3. Then I created my function and I wrote Results (to make reference to my struct) and then I named it printResults and in the parameters I wrote Results(the struct)”&” w, which means that any variable that has “w.” will be directed to the struct.
  4. I declared my variables that the function will return (sum and number_lines).
  5. Then I declared the ifstream “file” and the name of my file in parenthesis which is “TextFile.txt”.
  6. Then I did an if loop and the condition was that if the file was open then i would do the following:
  7. A while loop and in the condition I included a getline (function that counts the number of lines) and inside the getline the parameter are the ifstream file and the string line.
  8. Then inside that loop I did number_lines++, which means that it will give me how many lines by repeating the loop while there are more lines.
  9. Then I declared an integer called NumOfChars and I assigned it to the length of the string line (line.length()) (length is another function of the  string library).
  10. Then I
    Screen Shot 2017-03-16 at 10.38.40 AM
    Screen Shot 2017-03-16 at 10.03.01 AM
    Screen Shot 2017-03-16 at 10.33.55 AM
    Screen Shot 2017-03-16 at 2.08.26 PM
    Continue reading "Multipart Data and Files"

WSQ09- Multipart Data and files

--Originally published at Programming the city

Hey there!, basically this code shows you whatever you wrote in a text file, it shows you line  by line the words of a text file.

First I added the fstream library, this allows you to read a .txt file. After that, I wanted the program to ask the user what song would he/she wants to see. The user has to type the name of the song in a string value, the if function reads the name of the song and finally, it sees the which file name is it and it opens the text.

The teacher told us to make a function, but this code Works the same way without a function needed.

Just look at the operations I made and how you open the file.

CODE:

aaa

Result:


#WSQ09 Multipart Data and Files 10/03/17 and WSQ09.cpp

--Originally published at Solving Problems with Programming

PICTURE OF ACTOR

So in this nine week class I started with doing this WSQ09. I started reviewing in creating and calling functions in C++.#Mastery06, #Mastery07, #Mastery16 Use of recursion for repetitive algorithms, #Mastery17 When to use what type of repetition in a program, #Mastery18 Creation and use of Arrays/ Vectors in C++.

Futhermore in this assignment we have two new mastery topics covered #Mastery19 Creation and use of strings and #Mastery21 Reading and writing of text files.

What I did for this numeric program is solving the problem to the user by writing a function that receives as parameter the name of a file (this would be a string value like data.txt) and this 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). I 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.

Hence, the resources I need it to solve this program are here:

ken bauer

How to convert string to char

C++ Tutorial for Beginners 43 – How to Read from a .txt file using C++

The following photograph shows the solution to this problem:

wsq9v1

wsq9v2wsq9v3wsq9v4

Picture of author

So at first I wrote the same structure of the program just did the same as what i did in Hello World: Second Class, Second Blog (Blog of the second class 12/01/17) and Hello World.cpp,  #WSQ01 Post Fun with Numbers 16/01/17 and WSQ1.cpp#WSQ02 Post Temperature 23/01/17 and WSQ02.cpp#WSQ03 Post Pick a Number 23/01/17 and WSQ03.cpp#WSQ04 Post Sum of Numbers 23/01/17 and WSQ04.cpp#WSQ05 Six Tutorial On To Functions 12/02/17 and WSQ05.cpp#WSQ06 Factorial

charactes
Continue reading "#WSQ09 Multipart Data and Files 10/03/17 and WSQ09.cpp"

Mesa para cuantos caracteres?

--Originally published at Adal´s Blog

La actividad de hoy consiste en introducir a nuestro programa un archivo de texto para que este nos devuelva cuantos caracteres tiene, al igual que cuantas lineas  




He dejado lineas de código en comentario para que puedan jugar con las diferentes formas de correr el código 


Paginas de ayuda: