C :: How To Count Lines In Dat File Using Fscanf

Sep 15, 2013

I need a way to count lines in a .dat file using fscanf. I must use a regex to check that the characters in the file are alphanumeric. In essence I need to count the ' ' character. I know fscanf ignores this character. I need to exit if it detects anything other than alphanumeric with the digit that is "problem" along with the line number. My .dat file is:

Code:
howard jim dave
joe
(
Maggie My fileCheck.c is: Code: #include "main.h"
int fileCheck(FILE *fp)
{
int ret_val;
int line_count = 0;
char file[BUFF];

[Code]...

The output I am getting is:

Code:
file opened Digit: ( is not an alphanumeric character on line: 5 Program will exit! File closed As you can see, the character "(" is not on the 5th line but the 3rd. It is the 5th "string."

I also wanted to show the regex I am using.

Code:
#define to_find "^[a-zA-Z0-9]+$"

How this can be accomplished?

View 5 Replies


ADVERTISEMENT

C :: Program Count Number Of Lines Of Read Text File

Jan 25, 2013

I have a .txt file that contains, together with a few characters, columns of values that I want to save in different files like is written in the program (file 1, file2, file3 - a with x, b with y, c with z). The pattern of the source text file is like this:

known_vector frame1 151 65 0 frame2 151.000763 64.538582 0.563737
known_vector frame1 152 65 0 frame2 152.000702 64.542488 0.560822
known_vector frame1 153 65 0 frame2 153.000671 64.546150 0.558089

Until now I could manage to split the files, but the output gives me only zeros. First the program count the number of lines of the read text file, then it should display the desired columns of double values in three other .txt files.I've got for the three .txt files columns like this:

0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

View 2 Replies View Related

C :: Program To Count Number Of Lines In Text File And Reverse Contents

Jan 25, 2015

C program to count the number of lines in a text file and reverse the contents in the file to write in an output file.

ex :
input
one
two
three

output:
three
two
one

View 6 Replies View Related

C Sharp :: How To Count Number Of Lines In XML

Feb 4, 2013

Below is the response XML my programmer is getting from business service -

<Data>
<Maintain>
  <AssetList>
     <AssetDetails>
          <AssetDescriptor> 
             <ns1:ParentProductCat category="ABC">ABC</ns1:ParentProductCat

[code].....

In above XMl if you check the ParentProductCat is prefixed with ns1:,ns2:,ns3:....

My XSLT code only checkes for the ParentProductCat but since it is prefixed with "ns*:" (* is integer) it fails to find it. So that in C# code replace function is used to replace ns*: a for loop is used for this and it goes until 1000

But now my response crossed the 1000 mark and this time the XML contains total 14500 lines.So I want to replace all ns:*

For this I want to count the number of lines present in the XML so that the for loop will run until that line number. how to do that?

View 2 Replies View Related

C++ ::  Program Shouldn't Count Comments And Blank Lines

Feb 7, 2015

My program does compile and counts the number of lines of code, which is LOC: 20. My issue this, according to my code instruction it should not count comments (//) and also shouldn't count blank lines of code. Unfortunately my code is counting // and white space.

What seems to be working is #, all three includes are not being counted. That’s a good sign. I’m using the same technique not to count #. Why isn't working for comments and blank lines? I’m pretty sure that my logic is correct. I think my problem is maybe syntax.

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

[code]....

View 4 Replies View Related

Visual C++ :: Program Shouldn't Count Comments And Blank Lines

Feb 7, 2015

I'm making some progress. My program does compile and output the number of line per code, but it shouldn't count comments and blank lines. I tried using (s.substr(0,2) == "//") as suggested but it didn't work.

This is my improved code:

Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int A = 3;
int main() {
string s;

[code].....

View 1 Replies View Related

C :: Count Lines In Input And Display Output In Terminal When Program Executed After Compilation

Feb 4, 2013

Code:

#include <stdio.h>
main() {
int c, n1;
n1 = 0;
while ((c = getchar()) != EOF)
if (c == '')
++n1;
printf("%d", n1);
}

I have a more complicated program I'm wishing to have display the output, however, to save some time I'm using an example of a shorter version. count the lines in input and display the output in terminal when ./program is executed after compilation. To count and compute lines, words and within arrays.

View 4 Replies View Related

C/C++ :: Loading File With Fscanf?

Apr 21, 2015

I am trying to copy the stats from the save file "lol.txt" to main. I posted what the contents of the file below, but for some reason the I get for output jiberish and I cannot seem to figure out why.

#define _CRT_SECURE_NO_WARNINGS
#define MAX_NAME 31
#include <stdio.h>
int main(void){
char sName[MAX_NAME];

[Code] .....

View 5 Replies View Related

C/C++ :: Read Name / Phone Number And Address From A File - How To Use Fscanf

Nov 28, 2014

I am trying to read the name, phone number and address from a file ... Here is the code but it doesn't work and i don't know where is the problem ...

#include <stdio.h>
#include <stdlib.h>
struct PhoneBook{
        char name[50];
        int phoneNumber;
        char address[50];

[Code] ....

And here is the contents of the file:
jack 01014 jgd

Moh 02925 Tyeu

When I run it on dev c++, a message appear saying that the prog has stopped working and no output appears. I want to put the name in user[n].name, the phone number in user[n].phoneNumber and the address in user[n].address

View 1 Replies View Related

C++ :: Count Number Of Words And Lines Then Print All Words

Dec 20, 2013

I have written below program to count number of words and lines and print the all the words.

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

[Code] .....

Its compiling fine but when executed its displaying I infinite times...

View 3 Replies View Related

Visual C++ :: Program That Count From 1 To 12 And Print Count And Its Square For Each Count

Dec 16, 2014

4.1 Write a program that will count from 1 to 12 and print the count, and its square, for each count.

4.2 Write a program that counts from 1 to 12 and prints the count and its inversion to 5 decimal places for each count. This will require a floating point number.

4.3 Write a program that will count from 1 to 100 and print only those values between 32 and 39, one to a line. Use the incrementing operator for this program.

View 2 Replies View Related

C :: Reading From A File And Skipping Lines To Find String In File

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

C :: Fscanf Data And Delimiters

Dec 7, 2013

How would I fscanf this piece of data?

Delimiter is --> :
VS1234567890654327:Rob Fordfirst:001:200
VS1234567890654312:Steven Harper:200:010

Code:
while(3==fscanf(filename, "????", &string[size], &name[size], &number1[size], &number2[size])) {
//printf("%s - %s - %.3d - %.3d", string[size], name[size], number1[size], number2[size]));
size++;
}

View 6 Replies View Related

C :: Fscanf Data To Arrays

Apr 17, 2014

I am a very fresh c programme user, in fscanf data to arrays. Somehow it just doesn't gets recorded, wonder what is wrong with my code? When I printf the array it always ends up with random numbers.

Here is my code:

#include <stdio.h>#define SIZE 2
#define count 2
int main()
{
[code]....

and the file i am trying to read from :

Code:
1, 2, 3, 4
5, 6, 7, 8
8, 9, 10, 11

I am trying to scan a list of 3 by 4 numbers and the 4 numbers of each column to the 4 different arrays, so if I read from the data above, i get an array with numbers 1,5,8; another with 2,6,9 and so on. Is it how its done in my code?

View 2 Replies View Related

C :: Erase From TXT File Lines That Contain One Name?

Feb 4, 2013

Well, I have a .txt file that contains, together with a few characters, columns of values that I want to save in different files like is written in the program (file 1, file2, file3 - a with x, b with y, c with z). BUT, I don't want the values from the lines with the saying "interpolated_vector" to be printed in any of the three files.

What I have is the code below, that has the code that creates the three new files with the columns of values that I want. It is working fine!

Code:

#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
int i, count;
double *a, *b, *c;
double *x, *y, *z;
char tag[5][255];

[Code]...

I've tried and tried, but couldn't make it work properly. I could only erase one line with "interpolated_vector" using fgets, but all the other lines below this were printed into the three new files (file1, file2, file3).

View 5 Replies View Related

C++ :: Counting Lines From A File

Apr 23, 2013

How I would go about counting lines from a file at the same time as extracting words? I need to know the line number to output what line the word is misspelled on. I tried getline and using sstreams but I could not get it to work.

void SpellCheck::checkValidWords(const string& inFileName) {
string eachword;
ifstream istream;
istream.open( inFileName.c_str() );
if ( !istream.is_open() ) {

[Code] .....

View 3 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++ :: Read Only Certain Lines From CSV File

Oct 21, 2014

I am trying to read lines from a CSV file and put the values into vectors. The CSV contains a large amount of data and I would like the program to only read certain lines from the CSV into the vectors. An example of the data is below. In my code the user inputs a secid. I would then like the code to only read lines from the csv where the secid matches what the user inputs. The secid's are also not in order so I can't just use a while loop.

My current code is below the data.

secideffect_datecusip ticker
10131014MAY19972313510 AMZN
10131008MAY19982313510 AMZN
10131028NOV20002313510 AMZN
10131023JUL20012313510 AMZN
10196601JAN199663858510 NB
10196601OCT199806605F10 BAC

ifstream infile3("filepath.csv");
if (!infile3) {
cerr << "Couldn't open file!"<<endl;
return 1;

[Code] .....

View 1 Replies View Related

C++ :: Read Certain Lines From A File?

Aug 22, 2014

I want to read certain lines from a file. Let say if the line contains word "makes", the line will be loaded on the screen.how to modify this code.

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;

[Code] ....

View 1 Replies View Related

C/C++ :: Deleting Certain Lines In Txt File?

Dec 27, 2013

I was assigned to modify Game Lobby code. All the data can be saved in txt file and can be displayed when running the code..How can i remove or delete the certain lines of a txt file when running the cmd?.. I cant program it to be able to delete certain lines but i manage to program to delete the entire data, which is not what i want..

Here's the code so far...

#include <iostream>
#include <fstream>
#include<vector>
#include<cstdio>
#include<iomanip>
#include <string>
using namespace std;
class Player {
public:
Player(const string& name = ""): m_Name(name), m_pNext(0) {}

[Code] ....

View 3 Replies View Related

C/C++ :: Getting Data From File - Count Number Of Words In A Text File

Mar 27, 2014

I have a program I have to do that counts the number of words in a text file. I have tried the code on 2 computers now since my programming teacher told me the code was fine. Here is my code:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
ifstream infile;
infile.open("tj.text" , ios::in);

[Code] .....

View 4 Replies View Related

C/C++ :: Use Fscanf To Obtain A Set Of 14 Or So Strings Per Line?

Feb 25, 2012

I am trying to use fscanf to obtain a set of 14 or so strings per line, in a line where there are around 80 or so different sets of strings. I only need the first 14 and whenever I call scan f it starts at column 209 as opposed to column 1 where it should. Here's a sample of the code:

FILE *d;
d=fopen("t.dat","rb");
where a, n are all strings.
fscanf(d,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s",&a,&b,&c,..(etc)..,&n);

A sample of the Input file looks like this:
USB270.15385-29.63146 270.153847 -29.631455 2.966699e+03 -9.99 1.300391e+03 -9.99 -9.99 A-A-- 6.787463e+01 -9.99 1.555773e+02 -9.99 -9.99 10100 | ----- ------ ------ ------ | 0.373 13.554 12.928 12.670 AAA | ----- -------- - -------- - -------- - -------- - | --- ---------- - ---------- - --------- - --------- - --------- - ---------- -

View 7 Replies View Related

C :: Counting Number Of Lines In A File

Feb 2, 2015

it looks like a popular method for determining the total lines in a file is to read the entire file character by character in search of ' '. I have a file with 5 lines, but for some reason this code isn't finding any instances of ' '. Is this possible? Is there a better way to get the number of lines in a file?

The file looks like this:

Code:
NAME: John
FRIEND 1: Steve
FRIEND 2: Andrea
FRIEND 3: Ken
OCCUPATION: Programmer

Code:
FILE *f = fopen(currentFile, "r");
if (f == NULL) perror ("Error opening file.
");
int ch, lines = 0;
while(!feof(f)){

[Code] .....

View 2 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++ ::  Loading File With Multiple Lines

May 17, 2014

I'm trying to load a file that look like this:

IP:123.123.123.123
Port:12345

I can't figure out how to load multiple lines. I tried using , but I can't get it to work. Is there a better way of doing it than I am right now? This is my code.

char message[100];
int messageCount = 0;
for(unsigned int i = 0; i < file.size(); ++i) {
message[i] = file[i];

[Code] ....

View 1 Replies View Related

C++ :: Attempting To Get Data From Lines In A File?

Mar 30, 2014

For a project we are given a file with a couple of thousand lines of values, each line having 9 different numbers.

I want to create a for/while loop that, for each loop, stores the 8th and 9th integer of a line as a variable so that I can do some calculations with them. The loop would then move onto the next line, store the 8th and 9th numbers of that line as the same variable, so that I can do the same calculation to it, ending when I've run out of lines.

My problem is less to do with reading the file, I'm just confused how I'd tell it to take only the 8th and 9th value from each line.

View 5 Replies View Related







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