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
ADVERTISEMENT
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
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
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
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
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
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
View Related
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
Jun 11, 2014
Here's what I want to do:
So, basically the user imports a list of strings from a CSV file and the lines get read line by line and sorted in a listbox. If the user uploads the same file again that contains the same list of strings, a confirmation box with the following is supposed to be displayed:
"The list is the same. Are you sure you want to import this list of strings?" The options are yes, no, maybe.
If the user clicks on yes, all the strings get saved/put into the listbox regardless of whether or not they are repeated. No allows the user to to cancel the whole operation and return back to the screen for all samples. Maybe allows the user to individually pick which one they want saved or not. Maybe takes the user to a yes or no confirmation dialog box and it loops through all the strings in the csv file until the user has picked yes (accept) or no (deny). Only the confirmation box with yes and no should be looping.
What I have so far:
-Two confirmation boxes (I decided it would be easier for me to create two views and two viewmodels just for confirmation box setup - one with confirmation box of yes/no/maybe and one with confirmation box of yes/no).
-All the event handling is set and done.
-Reading the CSV file and checking for duplicates.
What I have problems with:
-When the call the confirmation dialogs...
Here's a snippet of what I have:
public void ReadImportedSamples(string filePath)
{
string line;
try
{
using (DDataContext context = new DDataContext())
{
using (StreamReader sr = new StreamReader(filePath))
{
[Code]...
So, I have two confirmation boxes, confirmationWindowSamplesListYN (only yes and no) and confirmWindowForSamplesList (with maybe).
Everything is set up fine. I am just wondering when the call the proper confirmation dialogs so that I can get the behavior above?
View 14 Replies
View Related
Feb 24, 2015
The requirement was to set the txt files information equal to a variable before and not to put it directly into the array.
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
const int MAX = 24;
struct applicant_info {
[Code] .....
the txt doc looks something like this: file name: jobs4.txt
View 2 Replies
View Related
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
Oct 10, 2014
I want to read a string of unknown length from stdin. I tried to follow the approach from this link.
[URL]....
My code is like this:
Code:
#include <iostream>
#include <string>
using namespace std;
int n;
cin >> n;
cout << "The value of n is " << n << endl;
string str;
getline(cin, str);
cout << "The entereed string is " << str << endl;
What I have noticed is that if I take integer input from cin (cin >> n in the above code before getline, the control does not stop on getline to take str as input from the console. If I don't do (cin >> n) before getline then the control stops on getline and takes the string as input.
What is the best way to read from console multiple strings of unknown length in combination with the integers?
View 1 Replies
View Related
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
Apr 17, 2014
I'm working on a project, and I'm trying to fill in various vectors from a given input file. The input file looks like:
<catalog>
<book id ="bk101">
<author>O'Brien, Tim</author> ....etc
My load vectors function looks like this: void load_vectors(vector<string>&id, vector<string>& author...etc)
I can't assume a limit on the number of books etc listed in this catalog, so I'm using the eof() function. However, I don't know how to write the loop to gather the correct strings and place them in the vectors.
Code: while(in.eof())
{ string text;
int index, index2;
getline(in, text);
int index = text.find("<author>");
int index2 = text.find("</author>");
a = index.lenght();
author.pushback[i] = text.substr(index + a, index2);
}
View 3 Replies
View Related
Aug 10, 2014
I have been trying to write this program that reads a file of courses and then stores only the unique courses in an array of strings.
for some reason it crashes while reading the 2nd line.
printf within the function are just for me trying to trace the problem.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** uniqueCourses(FILE *fp, int *wordCount);
int main()
[code].....
View 5 Replies
View Related
Mar 5, 2013
My objectives for this program is to open and read a file "dict.txt" which has the following data:
4
dog
pepper
marker
teapot
It needs to rotate the inner letters one to the right so you'd end up with "mrkear" etc, and then write those words into a new file "scramble.txt" one per line.I'm having a lot of problems understanding the program. Compiling is giving me a lot of warnings/errors.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXW 10 //max number of words in file
#define MAXS 81 //max size of string
}
[code]....
View 2 Replies
View Related
Nov 15, 2013
While I execute the fileprint function i was able to retrieve the record from the txt file. but at the end of the console im getting randoms number i have tried to understand what causing the problem. I have attached a screenshot....
Code:
void fileprint(){ Code: int c;
struct student{
long id;
char name[20];
int mid1;
[Code] .....
View 7 Replies
View Related
Oct 11, 2013
Write a program that reads several strings from a file. Display the number of words then display each word as shown in the sample below. Assume that the maximum number of string in the file is 100.
Sample Input (.in):
Smitty
Werbenjagermanjensen.
He
was
number
1!
Sample Output:
Total Number of Words: 6
Word[0] = Smitty
Word[1] = Werbenjagermanjensen.
Word[2] = He
Word[3] = was
Word[4] = number
Word[5] = 1!
View 1 Replies
View Related
Nov 17, 2013
I need making a function that is deciphering multiple strings from a .txt file. The function is supposed to remove all instances of the first three characters from the string.
An example would be from l.pjkjsdfl.p[)sdfslkl.p ------> jkjsdf[)sdfslk
Basically it removes "l.p" from wherever it is in the string.
I am new to C++ and I don't know where to start from.
View 1 Replies
View Related
Nov 26, 2013
Program background: Ticket Sales Details You will sell tickets in advance and at the door. Prices for buying in advance and at the door will be given. Also, the total number of tickets sold in advance will be given. Each guest will have a unique number. If there are n tickets sold in advance, then these guests will be numbered 0 through n-1. As the event starts, requests to buy tickets at the door may be made and these guests will be numbered sequentially, starting at the lowest unassigned number. The maximum number of guests will be 1000. The first line of the file contains the following three values, separated by spaces: Cost of the presales tickets (in dollars), Cost of the tickets at the door (in dollars), and the number of presale tickets. The first two values will be positive real numbers to two decimal places and the last will be a positive integer.
The second line of the file will contain one positive integer representing the number of auction items followed by a positive real value to two decimal places (at most) representing the minimum bid increment, in dollars. The first value is guaranteed to be 1000 or less and the second will be in between 1 and 50, inclusive.
The third line of the file will contain all the prices of the auction items, in dollars, separated by spaces, in order. Thus, the first price is the price of item 0, the next price is the price of item 1, and so on. These values will be real numbers represented to up to 2 decimal places.
The fourth line of the file will contain the three following positive integers pertaining to the raffle: the number of raffle tickets available, the cost of a raffle ticket in dollars, and the number of raffle prizes. (It's strange to have raffle tickets that don't cost a whole number of dollars.)
The fifth line of the file will contain each of the values of the raffle items, in dollars, separated by spaces, in order. Thus, the first price is the price if item 0, the next price is the price of item 1, and so on. These values will be real numbers with upto 2 decimal places.
The sixth line of the file will contain ten positive integers representing the number of each of the drinks 0 through 9, in order, that are in stock.
The seventh line of the file will contain ten positive real numbers with upto 2 decimal places representing the price of each of the drinks 0 through 9, in order.
The eighth line of the file will contain a single positive integer, numEvents, representing the number of events that occur at the charity ball. These events are split into two groups: actions by guests at the ball and awards given (raffle, auction, person, totalrevenue). All of the actions precede all of the awards. You will produce exactly one line of output for each event described. Here are the formats of each event that could occur:
If a patron buys a ticket at the door, a command will be on a line by itself:
BUY TICKET k
where k is a positive integer indicating the number of tickets bought at the door. These guests will be numbered as previously mentioned. You are guaranteed that the total number of tickets bought, including presales, will not exceed 1000. This is what I have so far and I cannot figure out why it wont calculate the total revenue. I am not completely sure if it is even accessing the if statement in main.
Code:
#include <stdio.h> #include <stdlib.h>
#include <string.h>
#define N 1000
[Code].....
View 4 Replies
View Related
May 5, 2014
I am trying to compare a string that i have entered with a set of strings that have already been stored in a file. I am using strcmp function but i am not getting the result.
Code:
printf("
Enter string:");
scanf("%s",&m);
ptr_file =fopen("abc.text","r");
[Code] .....
View 10 Replies
View Related
Apr 4, 2014
The first line of my input file is going to contain some number "T" which will represent the "combination length" of a list of random words. (In this case, they are Taco Bell items). The first number on the second line represents the number of unique items on the menu to get, and the second number on the second line represents the number of unique items that are supposed to be bought.
Basically the input will look like this: 2 3 2 taco burrito nacho
And the output looks like this: burritos nachos nacho taco burrito taco
This is what I have so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
int main(void){
int N, T, K;
char menu[N][20];
[Code] .....
What I am trying to get working right now is just to scan a file and put the strings into an array so then I can work on sorting the array. How can I add strings from a file into an array?
View 4 Replies
View Related
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
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
Aug 4, 2013
okay so this program is basically getting the files from user input then reading the original file and convert the string into a uppercase/lowercase form and save it into another file. Is there a way to save the string from the first file to a variable then convertin the string in the variable and copy that onto the new file? So far i have
Code:
#include <stdio.h>
#include <string.h>
int main() {
[Code].....
View 1 Replies
View Related
Jun 8, 2014
I need to develop a simple program, i have 2 variables (begin, end), and i need to search in a file, And extract the string between the Begin and the End variables to a new File, For Example:
my text file: file.txt:
some text here<StartHere>more text here</EndHere>text text
C++ Program:
//Declear 2 variables
strcpy_s(begin, string("<StartHere>").c_str());
strcpy_s(end, string("</EndHere>").c_str());
//And now, search in the Text file, And Extract the text between the begin string and the End string.
<...>
The Result should be: NewFile.txt with the content:
<StartHere>more text here</EndHere>
That's it!, Here is what i have for now:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
int ocurrences_count = 0;
int ocurrences2_count = 0;
char word[20]; //this array will save user input
[Code] ....
View 1 Replies
View Related