Jump to content

divide first column from a txt file

- - - - -

  • Please log in to reply
10 replies to this topic

#1
balambur

balambur

    Newbie

  • Members
  • Pip
  • 4 posts
hy guys,
I am a beginner in c and from 3 days I have a big question...
I have a file containing the folowing data:
5971 -44 976 -23
5981 -53 980 -23
5991 -50 978 -21
6001 -56 975 -21
6011 -58 971 -21
6021 -52 981 -24
6031 -49 974 -22
6041 -50 981 -31
6051 -47 978 -37
6061 -42 978 -38
6071 -37 977 -42
I need to divide the first column by 600 to convert in to seconds and write it out into a new file first column divided and the second, 3'th, 4'th leave it like is it now....
can anyone help me?
please???
Thanx a lot

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What part of it are you having trouble with? Dividing by 600, reading a file, writing a file, something else?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
How to write that program will depend on if you want C or C++.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#4
balambur

balambur

    Newbie

  • Members
  • Pip
  • 4 posts

Ancient Dragon said:

How to write that program will depend on if you want C or C++.

in C :)
some reason don't read the fille.... output is 0000 0000 0000 on all line....

#5
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Where is your code ? Sorry, but I'm not able to see your monitor.:cursing:
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#6
balambur

balambur

    Newbie

  • Members
  • Pip
  • 4 posts

Ancient Dragon said:

Where is your code ? Sorry, but I'm not able to see your monitor.:cursing:

#include <fstream>

int main()
{
    std::ifstream in_file("in.dat");
    std::ofstream out_file("out.dat");
    
    do {
        double x1, x2, x3, x4;
        in_file >> x1 >> x2 >> x3 >> x4;
        out_file << (x1 / 600) << " " << x2 << " " << x3 << " " << x4 << "\n";
    } while (in_file.good() && out_file.good());
}

Edited by ZekeDragon, 12 April 2010 - 03:08 AM.
Please use [code] tags (the # button) when posting code.


#7
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

balambur said:

#include <fstream>

int main()
{
std::ifstream in_file("in.dat");
std::ofstream out_file("out.dat");

do {
double x1, x2, x3, x4;
in_file >> x1 >> x2 >> x3 >> x4;
out_file << (x1 / 600) << " " << x2 << " " << x3 << " " << x4 << "\n";
} while (in_file.good() && out_file.good());
}

Is this your C program.. cool.. Its C++ dude...
you program works fine.. What's the problem..

#8
balambur

balambur

    Newbie

  • Members
  • Pip
  • 4 posts
#include <stdio.h>
#include <conio.h>
void main(void)
 {
   FILE *pointer_fisier;
   FILE *pointer_fisier2;
   int t,x,y,z,c;

       if ((pointer_fisier = fopen("peter6.txt", "r")) == NULL)
     printf("Error opening peter6.txt for input\n");
       else
    {
      fscanf(pointer_fisier, "%d %d %d %d", &t, &x, &y, &z);
          c=t/600;
      pointer_fisier2=fopen("datepout.txt","w");
      fprintf(pointer_fisier2, "t %d x %d y %d z %d",c,x,y,z);
      fclose(pointer_fisier);
      fclose(pointer_fisier2);
    }
 }


how can I cicle until EOF??? I know that is in C++ :D
now is in c and the problem is under ubuntu linux don't work so well in c++ so I need c

Edited by ZekeDragon, 12 April 2010 - 03:07 AM.
Please use [code] tags (the # button) when posting code.


#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
C++ works fine under ubuntu. conio, however is NOT cross-platform.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
Firebird_38

Firebird_38

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

Ancient Dragon said:

Where is your code ? Sorry, but I'm not able to see your monitor.:cursing:

That is hilarious. lol.

But, seriously, you should work on your telepresence skillz.

#11
omega

omega

    Newbie

  • Members
  • PipPip
  • 16 posts
try this function that i just wrote in C, there might be some errors ...

[SIZE=3][FONT=Courier New]char * read_line(FILE * fp)
{
    int counter = 0;
    char * line = 0;
    char * buffer = (char*) malloc(4096+1);
    char c = 0;

    if(fp == 0) return 0;

    while((c = fgetc(fp)) != EOF && c != '\n')[/FONT][/SIZE]  [SIZE=3][FONT=Courier New]buffer[counter++] = c;[/FONT][/SIZE][SIZE=3][FONT=Courier New]

[/FONT][/SIZE][SIZE=3][FONT=Courier New]    if(counter < 4096)[/FONT][/SIZE]
    {
[SIZE=3][FONT=Courier New]       line = (char*) malloc(counter);
[/FONT][/SIZE][SIZE=3][FONT=Courier New]       for(int i=0; i<counter; i++) line[i] = buffer[i];
       buffer[counter] = '\0';
    [/FONT][/SIZE]}
[SIZE=3][FONT=Courier New]
    return line;[/FONT][/SIZE]
[SIZE=3][FONT=Courier New]}
[/FONT][/SIZE]





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users