C/C++ :: Reading Lines Of Numbers / Characters While Checking For EOF?
Aug 30, 2014
Simple program to convert files to XML files. However I am trying to narrow down why my code is not working. I am reading a line that looks like "2001 Joe Dirt Los Angeles", then the next few lines are followed by text and the the fifth line looks like "---End of Description---".
So here is the file
2001 Joe Dirt Los Angeles
Home is where you make it
Best movie ever(but not really)
But seriously
---End of Description---
[space]Joe Dirt Los Angeles
[space]Home is where you make it
[space]Best movie ever(but not really)
[space]But seriously
My test code is skipping the date on the first line but replacing it with a space. Then it is skipping my fifth line which reads "---End of Description---". Now if I take the code out of the while loop and hard code in five lines of output (x5 getline(dataFile,line); cout << line << endl;) then my code works as expected. I get all of the information. Date and the fifth line.
It looks like when I am testing for the EOF it is also taking in the numeric value at the beginning of the first line. However, that does not explain why it is dropping that fifth line.
I think it comes from having to align the data in the output file. How to start this is to use a function to read in all the characters using a while loop. Then read in all the digits in a separate function. When it comes to outputting the data in the correct format I'm lost, so for now if I could figure out the functions that would be awesome. A final note we have not gotten to using strings so the I'm trying to use getc, fgetc, and ungetc.
Write a program to compute average grades for a course. The course records are in a single file and are organized according to the following format: Each line contains a students first name, then one space, then the students last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program will read data from this file and write its output to a second file. The data in the output file will be the same as the data in the input file except that there will be one additional number at the end of each line: the average of the students ten quiz scores.
The output file must be formatted such that first and last names appear together in a left justified column that is 20 characters wide. Each quiz score should be listed in a right justified column that is 4 characters wide, and the average should appear in its own right justified column that is 10 characters wide.
Note that if a student has fewer than 10 scores, the average is still the sum of the quiz scores divided by 10; these students are assumed to have missed one or more of the quizzes. The output file should contain a line (or lines) at the beginning of the file providing appropriate column headings. Use formatting statements to make the layout clean and easy to read.
I need to open a file that has multiple lines that look something like this, " black box 100.01 33.5 78.93". What this shows is the coordinates of the black box. Lines will have different objects with different coordinates. I need to create a loop that will read this file and tell me when a black box is found by displaying a message. I don't need to know how to create this file. I don't need to display the entire file but rather have it search for black boxes.
I want the code to search for the first occurence of the word "character," and start counting the lines from that line until it hit the first occurrence of the word "story."
Right now, I am only getting a counter value of 1.
I am writing a program for my class in C++. For this program we are required to use different fuctions and prototypes outside of main. We have to determine the number of characters, lines, sentences, digits, words, etc. in a particular function the user types in.
I have a file that is like the following, with patterns of pipes in it:
Code: ||| || | | | ||| | | | || || ||
I have to consider each row of pipe characters to be in blocks of 3 characters each (e.g. - positions 1-3, then 4-6, etc, etc...) but I have to capture all of the pipes, in sequence, like so:
positions 1-3 for lines 1-3, then positions 4-6 for lines 1-3, etc, etc...
How to get this done besides writing severely redundant control structures like loops, one after the other?
wrote this program to check if a string is an integer. It checks for + or - sign at the front of it, but it spat out some errors.I think I broke it.Here is the code:
Code:
#include<stdio.h> #include<ctype.h> #include<stdlib.h> int getInteger(char*); int main(void) { char str[99]; int x; }
I have to write a program (on linux) which will count character, words and lines like wc linux command. I'm trying to write this for last 3 days... First part of app I did and it works fine - command line options to choose. Then I've got a function read_file which I have to use to read a file. One of the options is to get the file name from user and if user will not type any name then the standard file is ubuntu dict file /usr/share/dict/words, this is not working as well...
Counting characters and lines is working fine but because I don't know how to get text from read_file wrote code to read file interior this functions. Words counting is working partly - everything is fine until there are two or more spaces, tabs one after another then counts extra words. Finally I need child processes in words and lines counting functions. Parent process should waits for all childs to finish and should be pipes to submit character counts back to parent process. How to do all this things with processes...
Code:
#include <getopt.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <errno.h> /*size of character buffer to read in a file. */ #define BUFFSIZE 1000000
I'm doing an exercise that prints all input lines that are longer than 80 characters. I rather not use any libraries so I decided to write my own function that counts characters to use it in my main program. However when integrate things my function returns zero all the time.
Here is my full code:
/* Exercise 1-17 Write a program to print all input lines that are longer than 80 characters */
#include<stdio.h> /* Declarations*/ #define MAX_STRING_LEN 1000 int count_characters(char S1[]); int main() {
[Code] .....
So I was trying to debug my count_characters() function and this is the code if I was to run it seperately:
Code: #include <stdio.h> /* counts character of a string*/ main() { int nc = 0; int c; for (nc = 0; (c = getchar()) != ' '; ++nc); printf("Number of characters = %d ", nc); }
Here is what I'm trying to accomplish (it is a rather simple program): A classroom of students are to grade a certain number of other exams. The exams should be distributed equally and RANDOMLY, every student should receive the same number of exams, and no student should receive their own exam to grade. The only problem I have is to generate unique random exams for each student. Right now, I have it set to where each exam is distributed the same number of times, every student gets the same number of exams to grade, and no one gets there own. However, I don't have any parameters that prevent one student from getting the same exam multiple time.
Here is an example output:
Student 1 will grade: 4 3 2 5 <- CORRECT OUTPUT (no exam appears more than once) Student 2 will grade: 5 5 5 1 <- exam 5 appears three times Student 3 will grade: 4 2 2 2 <- exam 2 appears three times Student 4 will grade: 3 3 1 1 <- exams 3 and 1 each appear twice Student 5 will grade: 1 3 4 4 <- exam 4 appears twice (each exam appears four times and every student is assigned four exams. no one gets their own)
Here is my code (area of problem is close to the bottom):
#include <stdio.h> #include <stdlib.h> #include <time.h> void create_class (int); int main (void) { srand(time(NULL));
[Code] ....
I tried keeping the exams for each student in the array exam and then checking each one every time I generate a number, but that didn't work.
I am trying to read lines from a .txt file like this:
// "file.txt" This is the first line. This is the second line.
To do this, I tried using the following code:
int main() { std::ifstream data; data.open("file.txt");
[Code] ....
The output I want is:
This is the first line. This is the second line.
However, all I am getting is:
This is the first line.
I can't quite see why this isn't working. I am decently familiar with file streams in C++, so I thought I would know what I was doing, but apparently not.
The first part of the exercise is to read the lines from a file placing each line into an array. I thought my code looked correct however nothing but garbage prints out. Here's my code.
----------------------------------- 45(here is space)string and other shit is here 454(here is space)string and other shit is here 4121(here is space)string and other shit is here 77(here is space)string and other shit is here 45545(here is space)string and other shit is here 1122(here is space)string and other shit is here -----------------------------------
how do i get exactly that number in every line start? and compare but i jus tneed to get them to variable so ?
I have a file that I need to read in blocks. I need to read in n lines at a time, do some processing, and then read in the next block of n lines until the file is done. I know the size of the block, but not the number of lines in the file unless I check that first. Normally I would read in like,
Code: // declarations string new_input_line, input_file; // create an input stream and open the input_file ifstream input_file_istream; input_file_istream.open( input_file.c_str() );
[Code] .....
// process through data_block
With this approach, I'm not sure how I would keep looping to read the next block until I hit the end of the file without knowing how many lines are in the input file. I could process the file to find that out, or get that number from bash and pass it it as an argument, but it seems like that shouldn't be necessary.
I could also read in and store the entire file as a vector of string and then process through it afterwords, but that would not be a very efficient use of memory.
if i start reading individual characters from a text file having the following content: "music, Indian classical dance, and other aspects of Indian culture.It is also a movement with chapters in over 200 towns and" what are the characters that will be read after the word 'culture.' ?
Carlitos, Gauleses , Terra das Vagabundas, SN., 350, 12, 5.83 EMPIRE STATE, Gauleses , EMPIRE STATE, FÊNIX™, 298, 12, 7.00 bigorna, Gauleses , Aldeia d bigorna, DDT@D, 318, 12, 10.44 akemif, Romanos , Aldeia Akemif, DDT@D, 19, 12, 13.04 Black Mason, Gauleses , Kindorim, DDT@D, 424, 12, 15.03
[Code] ....
I get this data from a site and I copy paste them to create a .txt file for input (I do this my self, no program involved). When the program runs it ends up like this on netbeans:
0 [main] program 2336 open_stackdumpfile: Dumping stack trace to program.exe.stackdump
When I type the input file my self it works fine. When I copy the data from the site that the problem occurs, it looks like those strange characters are causing the problem (notice the ????? are some drawings on text).
i have a string which is n characters long. i need to read say 20 characters at a time, wait for the user to type OK and then send another 20 characters. wait for the user to type OK and send 20 characters again until we get to the nth character.
This program is suppose to read a matrix file , and the first getline is suppose to get the file header but it appears that 'line' doesn't take in any value other than empty thus causing all the problem , I tried to put cin.getline() in front of it to take away the /n created by the cin before it , but it doesn't work . When I debug the program when the arrow points to the string line , this error appears
line<Error reading characters of string.>std::basic_string<char,std::char_traits<char>,std::allocator<char> >
I tried to initialize string line=NULL too , doesn't work either.
So I am supposed to create a program that reads a file and replaces "<" with "<" , ">" with ">" , "&" with "&" , and " " " with """.........
The program takes a file "test.txt" and reads it, replaces the characters above with the corresponding strings, and then writes the output to "scrubbed.txt".
A sample input would be: <something>
and the output would be: <something>
for some reason I am getting garbage In my new file. What am I doing wrong with the code?