help with c program to read each line from text file, split line , process and output
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Please post source code in [CODE] tags so we can all read it the way you wanted it to look.
Your one-line sample leaves some questions. If all lines in the file contain the same formatting with respect to the number of whitespace-delimited numeric fields, then fscanf() is your easiest route.
i ended up working out a different not so C like solution but i am still stuck.
look at this now:
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
int main()
{
std::ifstream f("myfile.txt");
// Read all lines
std::string line;
while(std::getline(f, line))
{
//assign variables to values
std::istringstream tmp(line);
std::string pic,tl1,tl2,br1,br2,le1,le2,re1,re2,n1,n2,m1,m2;
tmp >> pic>>tl1>>tl2>>br1>>br2>>le1>>le2>>re1>>re2>>n1>>n2>>m1>>m2;
//here i need to convert to int and work on the variables to output a new textfile with different data.//
You have specified three integer variables to be printed in your format string, but only two are given as arguments. This will result in undefined behavior.
--- rod.
or if you wanted to be strictly C malloc an array and reallocate it when you approach the limit of how much you have allocated for.
whatever you do you should probably use a dynamic storage mechanism, unless you know that you will ALWAYS have the same number or less of elements per line.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
Here's a example program that will parse as many lines of text you have in one or more files; it displays the tokens (individual "word") on the standard output although you could assign a token to a variable if that suits your needs. It's C -- not C++ -- but there you are. Hope it helps some.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif
int main (int argc, char *argv [])
{
char buf [BUFSIZ]; /* a buffer */
char *tokptr, *strptr = buf; /* a pointer to the buffer */
int c; /* general-purpose */
int error = FALSE; /* error flag */
int vopt = FALSE; /* verbose option */
FILE *in; /* input file */
/* process the command line arguments */
while ((c = getopt (argc, argv, "?v")) != EOF) {
switch (c) {
case '?':
error = TRUE;
break;
case 'v':
vopt = TRUE;
break;
default:
(void) fprintf (stderr, "getopt() bug\n");
exit (EXIT_FAILURE);
}
}
/* any errors in the arguments, or a '?' entered...*/
if (error) {
(void) fprintf (stderr, "usage: %s [-v] input_file...\n",
argv [0]);
exit (EXIT_FAILURE);
}
/* now process any arguments supplied... */
while (optind != argc) {
if (vopt) {
(void) fprintf (stderr, "Processing %s...\n", argv [optind]);
}
/* open the input file */
if ((in = fopen (argv [optind], "r")) == (FILE *) NULL) {
(void) fprintf (stderr, "%s:\tcan't open %s\n", argv [0], argv [optind]);
exit (EXIT_FAILURE);
}
/* get a line from the input file */
while (fgets (buf, sizeof (buf), in) != (char *) NULL) {
/* we have to point to buf */
strptr = buf;
/* take the line apart */
while ((tokptr = strtok (strptr, " \n")) != (char *) NULL) {
/*
* could assign tokptr to a varialbe here
* instead of just displaying it
*/
(void) fprintf (stdout, "%s\n", tokptr);
/* null the pointer */
strptr = (char *) NULL;
}
}
/* close the input file */
if (fclose (in))
(void) fprintf (stderr, "%s:\tcan't close %s\n", argv [0], argv [optind]);
/* bumpt the counter for next file */
optind++;
}
return (0);
}
here is the code.
it works fine until the 15th line.
after that the loop stops. i did not take a look at it yet.
i will post the code when its debuged.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.