Open Side Menu Go to the Top

11-07-2011 , 09:45 PM
right so ive been given a task to change in a file everytime ascii #10 (linefeed) to print out "\n" instead of a new line feed. Also to do the same for some other ascii numbers...

at the end of the file, i have to say whether the file is mac, dos or unix. You can tell because mac uses ascii #13, unix uses #10 and dos uses #13 follwed straight by #10 to create a new line.

the hwk says 'you can’t know when you see a CR (ASCII code 13)
whether it is a Mac line ending, or the beginning of DOS line ending.' so i need to implement something into my program so it can do this.
can anybody give me any hints/tips on how to do this?? (rather you not give answer but strongly push me towards a direction so i can learn myself)

what i need is something:
if (ascii #13 is followed by ascii #10)
printf(" this is dos file")
else if(uses #10)
printf(" unix file")
else (uses #13)
printf(" mac file ")

but in c code implemented to what i have below


my code atm
Spoiler:
Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
        int c;

        while((c=getchar()) != EOF)
        {
                if(c == 10)
                {
                        putchar(92);
                        putchar(110);

                }
                else if(c == 13)
                {
                        putchar(92);
                        putchar(110);

                }
                else if(c == 8)
                {
                        putchar(92);
                        putchar(98);
                }
                else if(c == 9)
                {
                        putchar(92);
                        putchar(116);
                }
                else if(c ==92)
                {
                        putchar(92);
                        putchar(92);
                }
                else if(c<=32)
                {
                        putchar(63);
                }
                else
                {
                        putchar(c);
                }

        }
}

Last edited by ElPonchis; 11-07-2011 at 09:56 PM.
C progamming hwk (pretty basic) Quote
C progamming hwk (pretty basic)
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
C progamming hwk (pretty basic)
11-07-2011 , 10:06 PM
Quote:
Originally Posted by ElPonchis

what i need is something:
if (ascii #13 is followed by ascii #10)
printf(" this is dos file")
...
i dont know if this is the solution or not, but if you've determined that the above is the solution for what you need, then you you'll need to grab two characters back to back and then compare both. right now your while loop is only grabbing one character and checking what it is

so something like

Code:
get char
if (char = 13) 
{
   get nextchar 
   if (nextchar = 10)  
   {
      printf "13 followed by 10 is dos newline"
   }
   else   
   {
      printf "13 followed by anything other than 10 is mac newline"
   }
}
else if (char=10) 
{
   printf "10 is unix newline"
}
...

Last edited by greg nice; 11-07-2011 at 10:11 PM.
C progamming hwk (pretty basic) Quote
11-07-2011 , 10:08 PM
hmmm. seems like a good spot for switch / case. 92 and 110 definitely inferior to '\\', 'n'. For mac/dos, how about another variable to remember stuff?
C progamming hwk (pretty basic) Quote
11-08-2011 , 12:15 AM
ok so both of your suggestions have helped me

greg: i didnt use nextchar etc as we havent used it yet in lectures. however your code helped me with the the end of the programme
edit: ohh nextchar aint actually something included, you just meant, literally get the next character ha my bad


chips: i changed it to the actual characters and not ascii numbers and used two more variables.

however, if i run a DOS file through this, it would print '\n\n' instead of '\n' due to my if(c ==10) and else if(c == 13) but i need them both to increment the count
any suggestions of how i can overcome this?

edit: i should of said, in lectures we have only gone through; functions, variables, loops, pointers. scanf, and just starting arrays. So 'we' dont know about switch/case atm

Spoiler:
Code:
#include <stdio.h>
                 
int main(int argc, char *argv[])
{
        int c;   
        int lfunix = 0;
        int lfmac = 0;
        while((c=getchar()) != EOF) 
        {
                if(c == 10)
                {
                        putchar('\\');
                        putchar('n');
                        lfunix++;
                }   
                else if(c == 13)
                {
                        putchar('\\');
                        putchar('n');
                        lfmac++;
                }
                else if(c == 8)
                {
                        putchar('\\');
                        putchar('b');
                }
                else if(c == 9)
                {
                        putchar('\\');
                        putchar('t');
                }
                else if(c ==92)
                {
                        putchar('\\');
                        putchar('\\');
                }
                else if(c<=32)
                {
                        putchar('?');
                }
                else
                {   
                        putchar(c);
                }
                 
        }
        if(lfmac > 0)
        {
                if(lfunix == lfmac)
                {
                        printf("\n\n\nthis file is DOS file\n");
                }
                else
                {
                        printf("\n\n\nThis file is a Mac file\n");
                }
        }
         if(lfunix > 0 && lfunix != lfmac)
                {
                        printf("\n\n\nThis file is a Unix file\n");
                }
                 
 
}

Last edited by ElPonchis; 11-08-2011 at 12:41 AM.
C progamming hwk (pretty basic) Quote
11-08-2011 , 12:41 AM
Quote:
Originally Posted by ElPonchis

greg: i didnt use nextchar etc as we havent used it yet in lectures. however your code helped me with the the end of the programme
"nextchar" was just pseudocode. none of what i suggested was real code

Quote:
however, if i run a DOS file through this, it would print '\n\n' instead of '\n' due to my if(c ==10) and else if(c == 13) but i need them both to increment the count
any suggestions of how i can overcome this?
yes, my suggestion above, to either get the next char at the time you encounter ascii13 and check if that next char is ascii10, or you could store the current char right at the end of the while loop, and then back reference it during the next iteration:

(pseudo code)
Code:
int c;
int prevchar;
...
while ( c = getchar() )
{
   if (c = 10)
   {
      if (prevchar = 13)
      {
         print "13 followed by 10 = dos"
      }
      else
      {
         print "non-13 followed by 10"
      }
   }
   elses..
   {
   }
   prevchar = c;
}
C progamming hwk (pretty basic) Quote
11-08-2011 , 01:05 AM
Quote:
Originally Posted by greg nice
"nextchar" was just pseudocode. none of what i suggested was real code



yes, my suggestion above, to either get the next char at the time you encounter ascii13 and check if that next char is ascii10, or you could store the current char right at the end of the while loop, and then back reference it during the next iteration:

(pseudo code)
Code:
int c;
int prevchar;
...
while ( c = getchar() )
{
   if (c = 10)
   {
      if (prevchar = 13)
      {
         print "13 followed by 10 = dos"
      }
      else
      {
         print "non-13 followed by 10"
      }
   }
   elses..
   {
   }
   prevchar = c;
}
haha yeh i realised what you meant just before you posted! (i did make an edit) ha

i THINK ive done it, will have to wait till tommorow to find out!
but thankyou so much!
C progamming hwk (pretty basic) Quote
11-24-2011 , 05:19 PM
Right so bump with another question, of course dont want to be told how to do it, but more tell me why this doest work!

i have to read int's from a file and store it in a two dimensional array, i really dont understand why this dont work. After every int in this file there is a comma. It just prints out random numbers all the time..im geussing im using fscanf totally wrong here as fscanf will check the whole file before the for loop repeats? so storing integars wrong! but i been told to use fscanf


Spoiler:
Code:
                                         

#define NO_STUDENTS 10
#define NO_MARKS 4
#include <stdio.h>



int main(char argc, char *argv[])
{
        int j = 1;
        int i = 1;
        int StudentMarks[NO_STUDENTS][NO_MARKS];
        FILE *fp;
        fp = fopen(argv[1], "r");
        for ( j = 1; j <= NO_STUDENTS; j++ )
        {
                for ( i = 1; i <= NO_MARKS; i++)
                {
                        fscanf(fp,"%d  ", &StudentMarks[j][i]);
                }
        }
        printf("%d \n", StudentMarks[4][4]);
        fclose(fp);
}

Last edited by ElPonchis; 11-24-2011 at 05:25 PM.
C progamming hwk (pretty basic) Quote
11-25-2011 , 12:34 AM
arrays are indexed from 0
C progamming hwk (pretty basic) Quote
11-25-2011 , 05:41 PM
Quote:
Originally Posted by ElPonchis
Right so bump with another question, of course dont want to be told how to do it, but more tell me why this doest work!

i have to read int's from a file and store it in a two dimensional array, i really dont understand why this dont work. After every int in this file there is a comma. It just prints out random numbers all the time..im geussing im using fscanf totally wrong here as fscanf will check the whole file before the for loop repeats? so storing integars wrong! but i been told to use fscanf


Spoiler:
Code:
                                         

#define NO_STUDENTS 10
#define NO_MARKS 4
#include <stdio.h>



int main(char argc, char *argv[])
{
        int j = 1;
        int i = 1;
        int StudentMarks[NO_STUDENTS][NO_MARKS];
        FILE *fp;
        fp = fopen(argv[1], "r");
        for ( j = 1; j <= NO_STUDENTS; j++ )
        {
                for ( i = 1; i <= NO_MARKS; i++)
                {
                        fscanf(fp,"%d  ", &StudentMarks[j][i]);
                }
        }
        printf("%d \n", StudentMarks[4][4]);
        fclose(fp);
}
fscanf doesn't use comma delimiters by default when reading input.

http://www.daniweb.com/software-deve...threads/334515
C progamming hwk (pretty basic) Quote
C progamming hwk (pretty basic)
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
C progamming hwk (pretty basic)

      
m