C :: Find Parse Error On Line 24 Reading From A File Using Structures
Mar 6, 2015
im trying to read employee information from a file using structures but im having problems with getting the file to open and read in the information
Code:
Write a programt that inputs (at most 50) records from a sequential file
#include <stdio.h>
#include <stdlib.h>
struct employee // employee structure definition
}
[code]....
View 13 Replies
ADVERTISEMENT
Jun 6, 2014
I'm using the Split method to parse line in a csv file. The following code works if there are no commas in the text of my data.
string csvLine;
string[] splitLine;
csvLine = "Jim Borland,1234,Never dreams in code";
splitLine = csvLine.Split(',');
But if I have commas in the some of the text as below I get then wrong output. So I need split on a commas which are not enclosed in double quotes.
string csvLine;
string[] splitLine;
csvLine = ""Jim,C,Borland",1234,"Never dreams in code"";
splitLine = csvLine.Split(',');
The output I want is:
Jim,C,Borland
1234
Never dreams in code
I found this :
If your strings are all well-formed it is possible with the following regular expression:
String[] res = str.split(",(?=([^"]|"[^"]*")*$)");
The expression ensures that a split occurs only at commas which are followed by an even (or zero) number of quotes ... and thus not inside such quotes).
Nevertheless, it may be easier to use a simple non-regex parser.
But the .split gives me an error and .Split doesn't work either.
View 5 Replies
View Related
Aug 17, 2014
I am trying to read a file line by line and then do something with the informations, so my method looks like this:
Code:
void open_file(char *link) {
FILE *file = fopen(link, "r");
if (file == NULL) {
fprintf(stderr, "Could not open file.
");
exit(EXIT_FAILURE);
[Code] ....
1) The first complain of valgrind is at the line where I use fgets and its telling me (invalid write of size x), but I have allocated my line to 56000 and the read line is shorter, why is there a write size error then :S?
2) at the line where I realloc where I try to shrink the space he's telling me: Address .... is 0 bytes inside a block of size 56000, But I know i need only this space so why is there a write over space error :S??
View 9 Replies
View Related
Sep 25, 2013
So I'm reading a file line by line and storing it backwards into a list. So if the file has has this format...
1
2
3
4
The code should store each line in a list as such...
4, 3, 2 ,1
Instead the code will store the last variable in all nodes. So the final list will look like this...
4, 4, 4, 4
Here is my code...
struct node *head = NULL;
int i;
while(read(in, &i, sizeof(int)) != 0) {
struct node *temp = malloc(sizeof(*temp));
temp->line = &i;
temp->next = head;
head = temp;
}
View 4 Replies
View Related
Apr 12, 2015
Im trying to read from a file, line by line, with no specified number of values in the file. Check out my code:
int main() {
string x;
ifstream fin;
int count = 0;
char ch;
fin.open("CWC_Master.txt");
if(!fin)
[Code] .....
Now, this works great! However, its skipping some lines. And I dont know why. For example: Lets say that the input file is:
superman toy
sm2335
19.99
batman toy
bm5532
25.99
aquaman toy
am6786
26.00
Where it should output the above, instead it outputs every other one. Like:
superman toy
19.99
batman toy
25.99
aquaman toy
26.00
How can I fix my code so that it SIMPLY(i say simply because I am still a beginner coder) can read line by line?
View 7 Replies
View Related
May 12, 2014
I have to read the information about logic gates from a file. The file format is shown below:
Code:
gateOne = NAND(inpA, inpB)
gate2=NAND(1,2)
3 = NAND(23,25,26)
As, it can be seen from the above structure that whitespaces are not same everytime. So, to deal with this situation, i am using boost library to remove all whitespaces from the line which is being read and then try to find the name of gate and its input. My code is given below which is able to correctly find the gate names and its first input...but my code is not able to find the second, third and so on input names.
Code:
//C
#include <stdio.h>
//C++
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <boost/algorithm/string/erase.hpp>
[Code] ....
View 3 Replies
View Related
Jun 9, 2013
I have to write a program that reads from a text file, which contains a list of stock hourly prices and the company names. Something like this:
78.52 82.56 75.10 71.97 Water Company
22.40 25.68 21.37 22.96 Mega Shipping Inc
There's suppose to one array of companies, where each company will be kept in a structure that contains: a pointer for the name, an array of the prices, and the average price for the day. The structures will be kept in an array of structures.
My question is, how do I read the data from the file and put the data from each line into the next structure in the array of structures? I read the numbers in fine. I just use:
Code: fscanf(fp, "%f", &companyAry[i].hourlyPrices[j]);
But my program crashes when I try to read the name.
Code: fscanf(fp, "%[^]", &companyAry[i].companyName);
I'm thinking it has something to do with the fact the companyName is a pointer.
My structure looks like this:
Code:
typedef struct {
char *companyName;
float hourlyPrices[NUM_PRICES];
float avgPrice;
}
COMPANY;
View 8 Replies
View Related
Nov 2, 2014
I'm reading the following file:
Osgood,Marcus 298542123 CHM FR mosgood@whatever.edu
Cronk,Melissa 873489021 BIO SR mcronk@whatever.edu
Pry,Seth 349908431 MTH SO spry@whatever.edu
Langlais,Susan 783323545 ME SR slanglais@whatever.edu
Davis,Nicole 987543345 PHY FR ndavis@whatever.edu
It's supposed to split it up into name, ID number, major, year, and email. The file reads it without any errors, and assigns name to the first part of the structure. However, ID gets assigned the ID, major, year, and email. Then Major gets assigned major, year, and email. Year gets assigned year and email, while email just gets assigned email. I don't know if it has something to do with the loop. For example, this is what I get what I print just the name and the ID.
Cronk,Melissa 873489021BIOSRmcronk@whatever.edu
Pry,Seth 349908431MTHSOspry@whatever.edu
Langlais,Susan 783323545ME
Davis,Nicole 987543345 PHYFRndavis@whatever.edu
Anyway. This is my function code for reading the array. I have it printing the ID number just to see if I can catch the errors earlier:
Code:
#include <stdio.h>
#include <string.h>
#include "functions.h"
void read_data(char filename[], studentinfo_t s[], int *size) {
FILE *infilep;
int countstudents = 0;
infilep = fopen("student_data", "r");
[Code].....
the name is reading correctly, but not anything else!
View 1 Replies
View Related
Oct 13, 2014
How to read a file line by line and then later access them by doing something like
Code:
lines[0] //Line number one
...
lines[100] //Line number one hundred and one
lines[100][0] //L
lines[100][1] //i
lines[100][2] //n
lines[100][3] //e
lines[100][4] //
lines[100][5] //n
...
View 13 Replies
View Related
Jul 27, 2013
First of all I need to know how to read from file.
Second, I need to read line by line from the file.
For example, the file contains:
8H 3S TH 9D 5S
5D KD 9H 4D 3D
8C TD 5D QS 2C
I wanna use array, so I need to store 8H in array[0], 3S in array[1], until 5S get stored in array[4].
I need to read the first line, the the second, and then the third.
View 3 Replies
View Related
Oct 7, 2012
I am working on a project were I have to read line form ( PLC ) programmable logic controller generated text file with lines like this
Circuit Value Current 2.33 4.32 5.55
there could be up to 3000 lines per txt file
I am using string stream to parse the line, for the sake of good programming I which to check weather first three values are string and last three values are actually floats raise or throw an exception if they are not ....
View 4 Replies
View Related
Mar 15, 2013
I'm trying to parse, or tokenize strings that follow the program name/command when used command line. I want to use the command followed by a sentence with a period. I know I need to user argv. But how can i parse the command rather than prompting the user for input. The input will be a sentence.
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <sched.h>
#define MAX_SIZE 50
using namespace std;
//function prototypes
void *vowels(void*);
void *constants(void*);
[Code] ....
View 3 Replies
View Related
Mar 6, 2015
how to read the next line in the file. I think I need to use a for loop, but not sure how to implement it.Lets say my input.txt file has three names:
Sally
Tom
Sugar
I can get Sally, but not tom as yet. fgets also gets me to the same place.
Code:
int main(){
char str_1;
FILE * ifp = fopen("input.txt", "r");
fscanf(ifp, "%s", &str_1);
printf("%s", &str_1);
return 0;
}
View 1 Replies
View Related
May 3, 2013
I wrote the below code to find the line with "abc" as my parameter to strTosearch. I expected to the line 1st line but, this function always match the 2nd line. What am I missing here?
I wanted "found" to be "abc def hgi SSS".
Code in main()
String found=GetstringColSamLine("mytext.txt", "abc");
File name - mytext.txt
abc def hgi SSS
abc ppp yyy
string GetstringColSamLine(string routerFileName, string strTosearch)
[Code] ....
View 3 Replies
View Related
Apr 3, 2015
I wrote this code to search words in the file and display if the program found the word or not. when i write the words in the file on the same line (without the endl) like this :
w_toFile << "gilbert";
w_toFile << "lara";
w_toFile << "rana";
i can search any word.
but when i write them with the endl, the program can find only the first word. what can i do to make the program find any word even if they are each on a line?
here is the full code:
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
using namespace std;
int main (void) {
string wordsearch;
[Code] ....
View 5 Replies
View Related
Nov 15, 2013
My file has these lines.I try to remove extra line in the end, in notepad/gedit, however, after saving it automatically adds up there.
one
two
three
Code:
void help::read_ref(const char* file_name){
std::ifstream file_handle;
file_handle.open(file_name);
std::string line,name;
getline(file_handle,line);
while(!file_handle.eof()){
[code]......
when I print size of map, it comes out to be an extra then what is should be.
std::map<std::String,std::string> ->is my map
on the contrary if I use Code: file_handle >> line; I get correct number of entries in map.
My doubt: getline, automatically finds and in last, it finds a , why it doesn't end the iteration?
I have faced this issue many number of times.
Code: line[0]=='>' I am making this check, then also, I get a null string as key in my map.
I have printed key with their index, using Code: map iterator .
View 7 Replies
View Related
Jul 11, 2013
I'm currently working on making a program that is run through a GUI run through the command line. The program basically takes an app file and a boot file and runs it through a bunch of functions and generates a new outfile. Anyway I'm new to C and can't figure out how to code it so I can type the two file paths into the command line and read them into the function. Is it possible to do this within the "if else" statement?
Code:
int main(int argc, char *argv[]){
const char * const SrcFilePath;
const char * const SRecordPath;
const char * const FopIspFilePath;
[Code] ....
View 4 Replies
View Related
Mar 6, 2014
finding the last line of the code? I have a program with two functions that open and display a joke.txt and punchline.txt file. My joke function (which reads the entire file) works fine. The punchline function, however, does not.
void displayPunch(fstream &file)
{
char ch;
string fileLine;
bool isLine = true;
file.seekg(0L, ios::end);
while(isLine)
{
file.seekg(-1L, ios::cur);
[code]....
The file, punchline.txt, has a bunch of garbage strings with one real string at the end. I need extract and display only the final line of that file.
View 5 Replies
View Related
Dec 10, 2013
I get the following parsing error for the code
Code:
#include <stdio.h>
int main()
{
int i=3,4;
printf("%d",i);
return 0;
}
what is wrong with the code? I have put initialize value as 3,4 just to understand what value does i gets assigned.
View 5 Replies
View Related
Oct 30, 2013
How would i get the total amount of elements From the input file(The .dat file) and then store them in a variable?Here is an example to show you what i want. If a line on the .dat file looked like this
1 2 3 4 5 6 7
How would i find the total number of elements? For example the total number of elements in this line would be 7.
View 9 Replies
View Related
Oct 28, 2013
I'm trying to make a program that will search for a line in a text file using a non default delimitor at the start of the line. An example line in the text file would be as follows:
F Mary Smyth, 19, United Kingdom
I have been able to use the find function to search for and return the 'F' character but would like it to then display the whole corresponding line. Is this possible with the find function?
ifstream readFromFile("data.txt");
string Destinations[1] = {"F "};
string data;
while(!readFromFile.eof()){
getline(readFromFile,data);
[code]...
View 2 Replies
View Related
Oct 19, 2013
I know how to do it in C, but in C++?
Here is my code. Let the variables be of type double.
Code:
while(infile) // Will read the last line twice
{
if(!N)//Read first line of file
{
infile >> d;
infile >> N;
infile >> epsilon;
infile >> t;
[Code] .....
View 8 Replies
View Related
Feb 7, 2014
i would like to read the content of a text file data.txt (line by line ) directly from the command line using this command: a.exe < data.txt.
What could be the c++ code to read/get the content of these lines (without using ifstream). The treatment of the lines is not a problem for me but i really don't know how to access the content of the file from the c++ code
View 2 Replies
View Related
Dec 23, 2013
My question is how can i read one line from a ".txt" file? To be more clear, i'm trying to write a hangman game. Therefore I'm trying to get words from a "wordlist.txt" file i created. I know a little bit about ifstream, ofstream and fstream. So my function i created to get a random line from this txt (not-completed yet since i don't know how to get that randomized line);
void grw() {
int line;
srand (time(NULL));
line = (rand() % 7972) + 1;
ifstream wordlist;
wordlist.open("wordlist.txt");
}
I created a variable called 'line' for the line number and randomized it (there are 7972 words in ".txt" file). So what i want to do now is to get the word on that line.
View 4 Replies
View Related
Feb 23, 2014
I have attached the file that I need to read into a data structure. In the example I am just printing it to the screen. This following code has worked for me before, but now isn't. I have tried in visual studios, and on unix, and neither are running it. I ultimately need it to run on unix.
Example file:
word
book
programming
Here is my function to read in the file.
ifstream file;
string line;
file.open("words.txt");
if(file.is_open()){
while(!file.eof()){
getline(file, line);
cout << line <<endl;
}
}
file.close();
View 7 Replies
View Related
Feb 9, 2012
Why this code outputs 7 lines (the last line twice) while the file contains 6 lines?
cout << "read msgfile
";
ifstream msgfile ("script1.msg");
while (msgfile.good()) //if not at end of file, continue reading {
// load vector with deffile
msgfile >> line;
vectormsgfile.push_back (line);
cout << line << "
";
}
msgfile.close();
View 2 Replies
View Related