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---

Here is example code to test the process

string line;
while(dataFile >> line) {
getline(dataFile, line);
cout << line << endl;
}

My returning output is

[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.

View 3 Replies


ADVERTISEMENT

C :: File I/O Reading In Characters And Numbers

Dec 1, 2013

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.

View 3 Replies View Related

C++ :: Reading From A File With Characters And Numbers

Apr 17, 2014

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.

View 3 Replies View Related

C++ :: Finding Number Of Lines Between Two Specified Characters

Feb 3, 2014

I am looking for a way to correctly count the lines between two specified characters/strings in a file. Here's the part I need work on:

getline( file, lines );
do {
if(lines.find("character")
{
++counter;
}
} while( !lines.find("story") );

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.

View 2 Replies View Related

C++ :: Counting Characters / Words / Lines And Digits

Mar 20, 2013

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.

View 1 Replies View Related

C++ :: Read A File And Break It Into Lines And Characters?

Jan 31, 2014

I am writing a code to read a file and break it into lines and characters but I am getting an error do not know the reason for it though.

here is my code

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

[Code].....

The first line is executing properly but when it goes to the second line I get an error "the program has stopped working"

View 1 Replies View Related

Visual C++ :: Looping Lines And Blocks Of Characters

Mar 17, 2013

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?

View 5 Replies View Related

C :: Reading String And Checking If Integer

Nov 23, 2013

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;
}

[code].....

View 2 Replies View Related

C :: Working With File And Counting Characters / Words And Lines

Dec 17, 2013

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

[code]....

View 2 Replies View Related

C :: Counting Function - Prints All Input Lines That Are Longer Than 80 Characters

Jan 10, 2014

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);
}

which works...

View 1 Replies View Related

C/C++ :: Checking Array And Generating Unique Numbers

Feb 11, 2014

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.

View 8 Replies View Related

C++ :: Reading From Specified Lines In File?

Jul 13, 2013

i want to read text between two indices in the file using file i/o operations in c++. HOw can i do it? for example, here is the text file:

1:
..
..
2:
..
..
3:
..
..
4:

in this text file i need to print only the lines between 2: and 3:. How could this be done?

View 1 Replies View Related

C++ :: Reading Lines From Text File

Jun 3, 2014

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.

View 1 Replies View Related

C++ :: Reading Lines From A File Into Array?

Sep 1, 2014

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.

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

[Code]...

View 2 Replies View Related

C++ :: Reading Number In Lines In Text File

Feb 27, 2015

lets say this is our textfile

-----------------------------------
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 ?

View 3 Replies View Related

C++ :: Reading Random Lines From A Text File

Apr 29, 2013

how to read random lines from a text file using "C", without repeating same lines.

View 10 Replies View Related

C++ :: Sequential Reading Blocks Of Lines From A File

Aug 14, 2014

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.

View 1 Replies View Related

C :: Reading Lines From A File And Storing It In Array From Typdef Struct

May 4, 2013

I'm trying to read in lines for a file and storing it in an array. This is what I have so far. Am I allocating the memory correctly?

Code:

typedef struct {
/* declartion of variables */
} App
typedef struct {

[Code].....

How do I access the array content? I'm suppose to strtok the contents from the lines stored in the array and use it again.

View 2 Replies View Related

C :: Reading Characters From A File

Mar 19, 2013

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.' ?

will it be '
' , '
' , 'I' and so on

View 2 Replies View Related

C++ :: Reading Odd Characters From A File

May 20, 2014

I have some files for input that look like this:

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).

View 13 Replies View Related

C++ :: Reading Characters From String

Apr 13, 2013

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.

View 3 Replies View Related

C/C++ :: Reading Characters From File

Dec 6, 2013

I searched for a code to read characters from file and i found this one

#include <stdio.h>
#include <stdlib.h>  
int main() {
   char ch, file_name[25];
   FILE *fp;  
   printf("Enter the name of file you wish to see

[Code] ....

but when i run it, and give the path of file in the cmd, this appears

stack around the variable 'file_name' was corrupted

given that i put the path in cmd as shown

D:CSEProject est1.txt

View 1 Replies View Related

C++ :: Error Reading Characters Of String

Feb 2, 2013

#include<fstream>
#include<Windows.h>
#include<cstdlib>
#include<iostream>
#include<string>
#include<iomanip>
#include<sstream>

using namespace std;

void add_matrix(int row1,int row2,int col1,int col2,double m1[30][30],double m2[30][30],double m3[30][30]);

[Code] .....

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.

View 1 Replies View Related

C++ :: Reading String 2 Characters At A Time?

May 10, 2013

may i know how do i read a string 2 characters at a time?

lets say i have a for loop like this

for(int i=0;i<stringLen;i++)
{
for(int j=0;j<???;j++)
{
//insert code
}
}

what i want to do is i want to read a string 2 characters at a time and store them into a vector.

View 3 Replies View Related

C/C++ :: Reading Unicode Characters From File?

Feb 28, 2012

I need to read Unicode characters from a file. The only thing I need to do from them is to extract their Unicode number.

For example if file has u I need to extract its corresponding Unicode number.

View 3 Replies View Related

C/C++ :: Reading A File And Replacing Characters With Corresponding String

Mar 27, 2014

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?

#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main(int argc, char **argv){
char *inFileName = NULL;
char *outFileName = NULL;
FILE *instream = fopen("test.txt", "r");
FILE *outstream = fopen("scrubbed.txt", "w");

[Code] ....

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved