I have to read from input file (which contains strings and integers) only integers, rearrange them to descending order and write to output file.
How should I do this?

Recommended Answers

All 8 Replies

If you the format of the data then you can use the fscanf function

If not use read to read the contents of the file into a char array and then implement a parser to parse the data

the text is most likely not formatted in any meaninful way and fscanf will be difficult if not impossible to use.

use fgets() to read the file line by line.
use strtok() to parse each element (or token) in the line by delimiting each element by the spaces (' ')
use strtol() to determine if element is an integer and convert it

Or
use fgets() to read the file line by line.
test each character to see it they are all digits
if all digits, copy the line to an array to be sorted and output.

There is no need to convert the numbers to ints although it might make the sorting easier.

Sorry for not posting this earlier but this is what I got so far

#include<stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct { int N;
                 char a[25];
                 int b[25];
        } andmed;

  
andmed read()
{
andmed x, size;
int i, y,j=0;
char andmefail[]="F1.txt";   
  
  FILE *F1;      
  F1 = fopen(andmefail, "r");       
  
  while (!feof(F1))  
  { 
        fscanf(F1,"%s",&x.a[i]);
        
        
       if(isdigit(x.a[i]))
       {
       y=atoi(&x.a[i]);
       
        size.N++;    
    printf("%d is a number \n",y);}
    i++;
  }
  printf("%d arabic numbers in file \n",size.N);
  
  fflush(stdin);
  fclose(F1);
  
  return (x);
}         

andmed sort(int a[],int size)
{
andmed y;
int i , j, tmp;
int arv;

for (i=0; i<size-1; i++) 

{
  for (j=i+1; j<size; j++)
      if (&y.a[i] < &y.a[j])    

    {

      tmp = y.a[i];         

      y.a[i] = y.a[j];

      y.a[j] = tmp;

    }

} 
    printf("%d ", j);
             
    printf("\nNumbers: ");
    for(i = 0; i<=size; i++){
          printf("%d ", y);            
    }
    
 getch();
return y;
}                   

int main(void)
{
    andmed row;
    int  i;     
    char andmefail[]="F2.txt";
    FILE *fout;

    fout=fopen(andmefail,"w");  
   row=read();
   for(i=0;i<row.N;i++){
                     printf("%d\n",row.b[i]);
                    fprintf(fout,"%d \n",row.b[i]);}
   getch();
}

The problem is that the main cant print the values from array.
Input file contains this:971 124 dgg 453634 fsd123 98 dft 798 ubc

You are using fread for reading from a file ... Check out the suggestions posted in the posts above

Thanks for everyone!
After total makeover everything works.

#include <stdio.h>
#include <stdlib.h>     
#include <string.h>     

void print(int i, int n, int b[])       
{
    char andmefail[]="F2.txt";
    FILE *fout;

    fout=fopen(andmefail,"w");  
   for(i=0;i<n;i++)
   {
                    fprintf(fout,"%d \n",b[i]);
   }
  
}
int main()
{
  int x;
  x=0;
  
   FILE *read;
   
   char andmefail[]="F1.txt";
   
   char a[100],*pos;
   int b[100],*ps;
   int i=0, j, tmp;
   int n=0;
   char buff[100];
   
   if((read=fopen(andmefail,"r"))==NULL)  
   {
      printf("\nCant open file\n");
      
      getchar();
      exit(1);
   }
   while(fgets(buff,sizeof(buff),read)!=NULL)  
   {
            printf("String from file:%s\n",buff);                                         
            pos=strtok(buff, " ");
            do
            {
              if (isdigit(*pos))
              {
              ps=atoi(pos);
              b[i]=ps;
              i++;
              n++;
              }
              pos=strtok(NULL, " ");
           }
           while(pos && *pos!='\n');
           printf("\nArabic numbers from file: ");
           for(i=0; i<n; i++)
           {
                    printf("%d ",b[i]);
           }
                    printf("\n");
for (i=0; i<n-1; i++) 
{
  for (i=(n-1); i>=0; i--)
  {
    for (j=1; j<=i; j++)
    {
      if (b[j-1]>b[j])
      {
        tmp=b[j-1];
        b[j-1]=b[j];
        b[j]=tmp;
      }
    }
  }
printf("\nArabic numbers sorted: "); 
for(i=0;i<n;i++)printf("%d ",b[i]);
  
}
}
   fclose(read); 
   print(i,n,b);
    
   getchar();
}

thanks for the follow up. glad it works for you :)

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.