im writing a program that opens a file, reads until it arrives at a colon, then jumps over the colon and reads the number after the colon
the txt file contains

pi:3.14159
feetPerMile:5280
0FinDegreesC:-17.8
Count these words:3
Space before number: 2.0
What about comments: 6.024e23


so i want to skip the colons

my code

#define LINESIZE 160
#define INFILE "config.txt"
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char buf[LINESIZE];
    
    FILE *fin;
    fin = fopen(INFILE, "r");
    
    while ( fgets ( buf, LINESIZE, fin) != NULL ) 
    {
     int i;
         if (buf[i] == ':');
           fputs ( buf, stdout ); 
           sscanf(buf+i+1, "%d:");
           printf("%d");
        printf("\n");
  }
      fclose (fin);
     return 0;
    }

when i run i get an error message
basically i want it to eventually print out

pi
3.14159
feetPerMile
5280
0FinDegreesC
-17.8
Count these words
3
Space before number
2.0
What about comments
6.024e23

why dont u use getc and putc instead of fgets , the code would be so much less complicated

#define INFILE "config.txt"
#include <stdio.h>
#include <string.h>

void main()
{
char ch;

FILE *fin;
fin = fopen(INFILE, "r");

do
{
ch=getc(fin);
if(ch!=':')
{
 printf("%c",ch);
}
else
{ 
 printf("\n");
}
}while ( ch!=EOF );
getch();
fclose (fin);
}

I hope this helps u out!

why dont u use getc and putc instead of fgets , the code would be so much less complicated

#define INFILE "config.txt"
#include <stdio.h>
#include <string.h>

void main()
{
char ch;

FILE *fin;
fin = fopen(INFILE, "r");

do
{
ch=getc(fin);
if(ch!=':')
{
 printf("%c",ch);
}
else
{ 
 printf("\n");
}
}while ( ch!=EOF );
getch();
fclose (fin);
}

I hope this helps u out!

it helps..but the problem is with this it only prints 1 charc...

It is in loop so it prints the entire text; I dont get u, it just prints one character at a time
try the code, it works :confused:

hmm looks that way but errors still come up:


D:\Program Files\PellesC\Projects\assignment2\main.c(37): error #1053: Disagreement in number of macro arguments.
D:\Program Files\PellesC\Projects\assignment2\main.c(37): error #2070: Insufficient number of arguments to 'getc'.
D:\Program Files\PellesC\Projects\assignment2\main.c(41): error #2001: Syntax error: found 'fclose' - expecting 'while'.
D:\Program Files\PellesC\Projects\assignment2\main.c(41): error #2001: Syntax error: found 'fclose' - expecting '('.
D:\Program Files\PellesC\Projects\assignment2\main.c(41): error #2001: Syntax error: found ';' - expecting ')'.

hmm looks that way but errors still come up:


D:\Program Files\PellesC\Projects\assignment2\main.c(37): error #1053: Disagreement in number of macro arguments.
D:\Program Files\PellesC\Projects\assignment2\main.c(37): error #2070: Insufficient number of arguments to 'getc'.
D:\Program Files\PellesC\Projects\assignment2\main.c(41): error #2001: Syntax error: found 'fclose' - expecting 'while'.
D:\Program Files\PellesC\Projects\assignment2\main.c(41): error #2001: Syntax error: found 'fclose' - expecting '('.
D:\Program Files\PellesC\Projects\assignment2\main.c(41): error #2001: Syntax error: found ';' - expecting ')'.

Are you sure you wrote the exact code? Because I am using Dev C++ and the program runs absolutely fine. So I am not sure why these errors are cropping up :eek:

lol im using pellesC thats why

which means theres no getch()

lol im using pellesC thats why

but does it matter which compiler your using? the syntax remains the same.
Oh ! so 'getch()' is not there :), do tell if the program works without getch()

if i change getch() to getc(fin) it returns p-other wise the errors appear

nvm that doesnt work either

nvm that doesnt work either

nvm? dont really know about pellesC :rolleyes:

In C++ you could use : as the terminating char to getline() to do the job in a whiff. To my knowledge fgets() is the closest you can get to getline() in C, though, and it doesn't have a terminating variable as an argument.

I would either read the file one line at a time with fgets() and then read the string one char at a time until I found :, or I would read the file one char at a time until I found :.

//pseudocode
void parseString(char[80] input, char[80] s, int & data)
{
   int i = 0;
   int j = 0;
   int len = strlen(input);
   char temp[80];

   while current char in input is not a colon
      assign current char in input to s
      
   when current char of input is colon
       terminate s with a null char
   
   ignore the colon

   read remainder of input into temp
   
   terminate temp with a null char

   convert temp from string to numerical value using sscanf() and store in data
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.