C/C++ :: Reading Strings From A Text File And Printing Them

Feb 26, 2015

I need a program that reads the number of lines of a file and then several (max of 20) lines of text from a .txt file so an example of the .txt file is: 2 This is the first string This is string number 2 So it first reads the 2 and then reads the two lines of text. It stores the text in an array of pointers. The code i have so far is:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>

[Code].....

It doesnt give me any errors but all it does is keep running and doesnt print anything out kind of like its in an infinite loop although i dont see how that could be possible.

View 1 Replies


ADVERTISEMENT

C :: Reading Formatted Strings From Text File

Nov 28, 2014

I wanted to improve the game from my last thread. I want to read and store a question in this format: Code: Who invented the BALLPOINT PEN?

A - Biro Brothers
B - Waterman Brothers
C - Bicc Brothers
D - Write Bothers

But when I use fgets() to read the string, it reads all the ' ' literally instead of outputting a real newline. Like if it read the above question, I want it to print something like this:

Code:
Who invented the BALLPOINT PEN?

A - Biro Brothers
B - Waterman Brothers
C - Bicc Brothers
D - Write Bothers

View 3 Replies View Related

C :: Reading And Printing In A Specific Format Strings Of Characters And Integers

Feb 23, 2015

I have been skimming and searching but dont know how to word the search just right to find what I need to know. I have written simple stuff with the support of tutorials like weight conversion or loops counting up to a set number. Nothing amazing. The other day I found out how to input and print a string rather than a single text character which i though was bad ass. I even got it to read multiple strings on a single line and found a way to read multiple lines. I can even format it to read both integers and characters on the same line as long as I know the predefined format.

On to my question... How do I read multiple lines with both carecters and integers. for instance:

nissan 1996
toyota 1998
or more comples like
nissan gtr 1996
toyota markii 1998

I want to use
int year;
char make[10]; maybe need to use char make[10][10]; for an array i would guess.
char model[10]; optional for the extra data

but reproduce what i read in a different order. say...
1996 nissan
1998 toyota
vice the original format.

this is what I have tried.

Code: scanf("%s %s", &make,&year);

//The way I seen to read multiple lines was on here

scanf("%[^/t]", %make);

But this wont let me separate the two into two differnet definded data types. Let alone use printf to display them in reverse order.

View 1 Replies View Related

C/C++ :: Reading File And Printing Stuff

Jun 5, 2014

void readpatient() {
int count = 0;
f = fopen("patient.txt","r");
while(!feof(f)) {
printf("
%d",count++);
p2=new patient();

[Code] .....

Here is what I've tried. I'm trying to read a file aND print stuff in it, but my while loop isn't ending up.. What should i do? It only prints patient name 1 time and after that it prints nothing.

removing system("pause");

results in an endless loop.

View 2 Replies View Related

C/C++ :: Reading Hex Values From File And Printing Them In Decimal

Dec 1, 2013

I am trying to read hex data from a file (not just hex values in text format). I was able to read it (02 EC) with "fread" into an char before but I need to change the Hex value into an integer in decimal.

I already read about strtol but I would prefer reading the hex Value into an integer.

Here is my code so far:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int itrack1[1];
int itrack2[1];

[Code] ....

View 11 Replies View Related

C :: Reading Strings From File

Nov 2, 2013

It appears that when you enter command line arguments or use fgets() to input a string you can assign that string to another string variable using the assignment operator. But when you read from a file, you can't do that, you get a segfault. It seems the only way to get around that is to malloc the string and use the strcpy function.

Code:
#include <stdio.h>
struct person {
char *names[2];
};
void readFile(struct person p){
FILE *file = fopen("names", "r");

[Code]....

View 3 Replies View Related

C++ :: Reading File Then Printing Data Onto Other File

Nov 7, 2014

I am reading a file then printing the data onto the other file. It is working, however when I check to see if each variable is being properly set after reading the file a issue arises.

Example of the file being read

Code:
Vehicle PV50CAN passed camera 1 at 05:33:26.
Vehicle W867BRO passed camera 1 at 05:33:29.
Vehicle KQ63ARU passed camera 1 at 05:33:38.
Vehicle K954ITQ passed camera 1 at 05:33:40.
Vehicle V220MXB passed camera 1 at 05:33:42.

[Code] .....

View 14 Replies View Related

C++ :: Printing From Open Text File?

Nov 14, 2013

I need to create a text file that looks like:

first line
second line
third line
forth line
fifth line
sixth line

I want to replace the third and forth lines with three new lines. The above contents would become:

first line
second line
new line1
new line2
new line3
fifth line
sixth line

How can I do this using c++?

never was taught file function in c++

View 3 Replies View Related

C++ :: Reading Strings From File To 2D Array?

Apr 27, 2013

I have a text file with scores and names.

I have to read the score into an array and the names into an arrays also

27,Tom Jefferson
23,Ozzie Osborne
18,Wilt Chamberlain
15,Marie Antoinette

I've gotten the score to display correctly, but i cant get the full names. My function only reads the first names

View 8 Replies View Related

C++ :: Reading Strings From A File With Spaces?

Aug 2, 2013

I need to write a program that reads a paragraph from a file (i dont know its length) and i need to read it with spaces

i thought of using getline() function but there were problems:

1) how to detect End Of File

2) how to use getline while reading from file

and thought of reading one character at a time but it didnt read spaces

View 4 Replies View Related

C :: Reading Matching Strings From A File And Deleting From Another

Sep 26, 2014

We are generating emails for freshman students, however, the system probably due to server overload fails to generate some emails. So I'm working on this c code( probably was a wrong choice) to search for regexp matching the generated emails and deleting from the list of students and propective emails.

I"m currently at the stage of making sure the code can find all the matching regexp from the student list file before deleting! But the code fails to read all matching regexp?

Code:
//Filename: SearchReplace.c
//Usage: This searches for lines in a file containing a particular work and deletes the lines.
//Licence: GNU P.L.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

[code] .....

What I'm missing. It recognizes some regexp and fails to recognise some. Been staring for hours!

View 4 Replies View Related

C/C++ :: Reading Strings To Char Array From Exe File

Feb 25, 2014

I am trying to read strings to an char array from an .exe file and then i would check some of the strings, but the problem is that the only thing that is read from the file is the first string (MZ) and an 'square' that is some incorrect character. I am using fread to read from the file. Here is my code:

FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = fopen("my_file.exe", "rb");
if( pFile == NULL) exit(1);
fseek(pFile,0, SEEK_END);

[Code] ....

(I want to read the whole file, its not that big)...

View 14 Replies View Related

C/C++ :: Reading Strings From A File And Determining If They Are Palindromes

Oct 17, 2014

I have to make a program to read a file with strings and determine if they are palindromes. The problem is that the program says that they're all palindromes.

int main() {
ifstream inFile;
ofstream outFile;
inFile.open("in.data");
outFile.open("out.data");
if (!inFile || !outFile) //to display error if input files are invalid

[code]....

View 3 Replies View Related

C++ :: Data File Handling Error - Character Strings Not Copied On Text File

Nov 24, 2013

I have this code for a computer project... (store management) but the character strings are not copied on text file..

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class Store {
public:
char *item_name[5];
store()

[Code] .....

Now when i run the program, it gives a error :::
ERROR
address 0x0

How can i write these strings to the text file?

View 2 Replies View Related

C :: How To Read A Text File Into Array Of Strings

Mar 3, 2013

In a program, I have a text file (called MyDictionary.txt) which has thousands of words in alphabetical order. I need to make a C program that reads in this text file and then makes an array called char Words[# of total words in the text file][length of longest word].

Aarhus
Aaron
Ababa
aback
abaft
abandon

View 6 Replies View Related

C :: Read Text File With Strings And Integers

Nov 16, 2013

i have prepared a code the read from txt file with values such integers and strings. but the code i have prepared reads only 1 line. how can i make the code to read multiple records from txt file.

input records example in txt file:
9009 James 90

Code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int c;
struct student{
}

[code]....

View 4 Replies View Related

C :: How To Make String Array From Strings In Text File

Mar 24, 2013

I want to make a string array from strings in a text file. Itry to do this but i couldn't do, where is my mistake?

Code:

#include <stdio.h>
#include <stdlib.h>
int main(){
char cumle[100],*c,*dene[50];
FILE *input;
input=fopen("input.txt","r");

[Code]...

View 1 Replies View Related

C :: Reading From Text File

Mar 11, 2013

extracting records from a text file based on the user input of a specific date. Below is a image of the Text file with Dates and records that follow. a code to extract records from file based on the day the user enters with month and year not necessary here.so it is based on the day the user enters, which the code will extract the records and if day is not found then "DATE NOT FOUND"

View 12 Replies View Related

C :: Reading Text From A File

Mar 12, 2013

I'm writing a code which has to read a text from a file. In this text, there are some symbol and numbers (ex, This is $19 of....). Those symbols and numbers should be replaced with the words from the other file. i.e.

transformed file

This is just an $19 of what the $5 should obtain.

And $19 should be replaced from association file

19 example
5 program

The result:

This is just example of what the program should obtain. I'd like to save the first file content into an array of strings and the same for the text of second file.

View 2 Replies View Related

C++ :: Reading From A Text File

May 9, 2013

I got an assignment to do...and in that i need to get the inputs from a .txt file....and im not getting how to read the data i want and skip the rest thrash.

Lambton:

Name: chocolate
demand rate: 50
setup cost: 200
unit cost: 5.2
inventory cost: 2

[Code] ....

Program is about a coffee shop with two branches and i have to keep track of the stock in the stores.

Lambton and Callaghan are the stores.

How can i read them ? I know how to open the file to be read..

View 2 Replies View Related

C :: Reading Numbers From A Text File

Feb 23, 2013

The program runs fine but i just want it to read the inputs from a text file rather than user manually entering it! what changes should i make in the codes..

Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
float friction,flow;

[Code] ....

View 4 Replies View Related

C++ :: Reading Matrices From Text File

Jan 2, 2014

I would like to program a simple finite element solver in c++ (I'm a relatively new programmer by the way). This is the issue, I have a text file with all the information of the model arranged in matrices; an input file would look something like this:

*nodes
1 0 0 0
2 10 15 0
3 17 2 0
4 -4 -1 0

*elements
1 1 2
2 2 3
3 3 4

*material
1 2.0e5 0.30 7.85e-9
2 7.0e4 0.21 2.11e-9

*loads
1 0 0 0
4 0 0 0

So, i want to read this text file and get each of the "*" matrices in its own array. The size of the matrices is not know.

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++ :: Path For Reading A Text File

Oct 13, 2013

I need to open a text file game_scores.txt and i can not figure out a way to.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {

[Code] ....

the code is used to read the text and take the first 4 pieces of info but i do not know how to do it. i have already created the text file and moved it into the source. i am using visual studios 2012. In 2010 the text file would show a relative path, it doesn't in 2012

View 10 Replies View Related

C++ :: Reading Columns From A Text File?

Oct 19, 2013

How can I read just the first column of this from a text file?

ORG 100
LOAD X
ADD Y
STORE Z
HALT
X, DEC 49
Y, DEC -80
Z, HEX 0000

I have

while (! sourceCode.eof() )
{
getline(sourceCode,symbolValue,'
');
sourceCode >> symbolValue >> codeWord >> value;
symbolTable << symbolValue << endl;
}

View 9 Replies View Related

C++ :: Reading Columns From Text File

Oct 19, 2013

How can I read just the first column of this from a text file?

ORG 100
LOAD X
ADD Y
STORE Z
HALT
X, DEC 49
Y, DEC -80
Z, HEX 0000

I have

while (! sourceCode.eof() ) {
getline(sourceCode,symbolValue,'
');

sourceCode >> symbolValue >> codeWord >> value;
symbolTable << symbolValue << endl;
}

View 2 Replies View Related







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