first we read something from the text files ( INT type only ),
then i want read till "\n" stop and inceasement a value,
examples,

int Attendance[10][10];
	ExistingFile = fopen("Attendance.txt", "w");
	if(!ExistingFile)
	{
		printf("\t\t   This File Cannot Be Open Successfully!!\n");
		printf("\t\t   Please Check It!!\n");
		exit(-1);
	}
	else
	{
		fprintf(ExistingFile, "1 0 0 1 1\n");
		fprintf(ExistingFile, "1 1 1 1 1\n");
		fprintf(ExistingFile, "0 0 1 1 0\n");
		fprintf(ExistingFile, "1 0 0 1 1\n");
		fprintf(ExistingFile, "1 1 0 0 1\n");
		fprintf(ExistingFile, "1 0 1 0 1\n");

		x = 0;

		while(x<6)
			for(i=0; fscanf(AppendingFile,"%d ", &Attendance[x][i]) != EOF; i++)
			{
				if(fscanf(AppendingFile,"%d\n",&Attendance[x][i]) != EOF)
					x++;			}

how can we do with that?

Recommended Answers

All 20 Replies

This is one way:

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

int main() {
  int i,j; 
  char a[]={"1 22 33 444 5555 5 4 3 2 1"};
  int n[10];
  
  for(i=0,j=0;i<10;i++) {
    n[i] = atoi(a+j);
    while(a[++j]!=' ');
    printf("\n %d: %d", i, n[i]);
  }
  printf("\n\n\t\t\t     press enter when ready");

  (void) getchar(); 
  return 0;
}

Don't expect a safety net with atoi(). You won't get anything except a 0 on error.

Check your compiler and see if it has the safer strtoi() (string to int).

I've had good weather and fair winds with atoi(), personally.

but i read from text file is only integer type..
no string or char types.. ~

The code you posted, is writing out strings of char's, not ints:

fprintf(ExistingFile, "1 0 0 1 1\n");

See those double quotation marks around your string of digits? That's a sure sign it's a string, in C.

but in text file, whatever you append or read or exist.. also using double quotation :-O

If I print out int's, then the file will have int's in it. When you print out strings of char's, then the file will have char's in it. EVEN if the char is a digit, it will still be a char digit, and not a true int.

The beauty of what the code I posted it, it doesn't matter whether the original data was a char string with digits, or real int's.

It takes them all in as char's, and THEN it transfers them into true int's.

Works either way. ;)

do you have any idea other than atoi() statement ?
because im still dont know how to use it..
and im read it from my text file after that i want assign it into my Attendance[..][..]

i also have seach in google..
most of them is using getline statement ?
is getline statement easier ? i mean not so complicated.

Getline is a function to make it a bit easier to get a line of text. In some languages, it's standard, but not in C. fgets() is not as simple as gets() (which is absolutely simple as pie), but gets() is NOT safe - all the hackers know how to use it's weakness to create havoc.

Like most things that you want to do, there's more than one way to do this. You want to use fscanf() for this?

Is the input.txt numbers, formatted like this?
1 2 3 4 5 6

(six numbers, each separated from the next by one space)

fscanf, because i want read from the text file..
so i must use fscanf to read it..
yeah..
inside my text file format is like that , seperate by one space but when end of the line is '\n'

Are there always 6 numbers in a line, or are there sometimes less or more than six numbers?

no always be 6..
depends on user...
if the user want to add one more week, so there have 7 number
that's why i put x<week in my first condition.

In pseudo code, for six numbers, this will do it. Change define C to whatever number of numbers per row you need.

include stdio.h

define R 2
define C 6

begin main() 
  declare:
  =========
  ints i,j, r,c, ok 
  char ch
  int a[R][C]  //R=Rows, C=Columns
  FILE * fp
  //end declarations
  //begin processing
  print("\n\n")
  fp = fopen("input.txt", "r")
  if(fp==NULL) 
    printf("\nError opening file\n")
    return 1
  end if

  set r and c to 0
  set ok to 1
  while((ok=fscanf(fp, "%d%*c", &a[r][c])) > 0) {
    if(ok less than 1)
      break
    print("%d ", a[r][c])
    ++c
    if(c equals C)
      set c to 0
      ++r
      putchar('\n')
    end if
  end while loop
  print("\n\n\n")
  fclose(fp)
  printf("\n\n\t\t\t     press enter when ready");

  (void) getchar() 
  return 0
end program

If input text has this input:
1 2 3 4 5 6
6 5 4 3 2 1

That is what the program prints up, and assigns to array a[r][c].

I left this in semi-pseudo code, because I see that you want to learn, and there's no better way to learn, imo, than taking pseudo code that works, and making it into a program.

ya now i understand your pseudocode..
the point is when your c == C, then you ++r.
which mean like this,

while(x<week) //assume that week is 6 and student is 5
	while(fscanf(AppendingFile,"%d ", &Attendance[x][i]) != EOF)
		if(i == student)
		{
			++x;
			i=0;
		}

but the program is stucking.. why? @@

oh i got it already..
i forgot to increament my i,
for i still another question..
why i can't run it if i change this
while(fscanf(AppendingFile,"%d ", &Attendance[x]) != EOF) into for loop ? ( i=0; i++)

You can always change a while loop into a for loop, but you need to include the right check for a stop condition - which you don't show at all.

oh..
i got it dude.. thanks alot..
but i want ask again..
if 12345 i set it..
but the next line is 123456..
so how to do with it ? must some statement to detect there is \n ?

When you scanf() for a number, whitespace char's are passed over. That's one of the advantages of getting input as a stream of char's, btw.

What you can do is change your fscanf() from %d to %d%c, format. Assign a char to %c, like normal &ch.

Now %c will put all the spaces AND the newline (when it reaches the end of the row), into that ch variable, and you can test it, and use that, for your end of row test

which mean \n is char type ?:-O
then the condition of after 2 while loop is the same ?
like

if(ch == '\n')
<my action>

Yes, exactly,

however: ;)

It's a bit complicated, because in Linux/Unix/and all binary files, '\n' is the newline, and that's that - simple.

In text mode however, *sometimes* you'll see a newline as a TWO char combination: CR (carriage return), and LF (line feed). So if you see an obvious problem with '\n' somehow being ascii 10 followed next by ascii 13, then you'll know why that is, and can move to handle it.

That happens because text file handlers "expand" a newline, into CR/LF combo's, which usually your C program will handle as one char OK, but always -- well, no.

Always is such a long time, eh? ;)

:D im still in studying..
so many of your words which i haven't learn one. =x
haha..
anyways.. thanks for the help dude. =D

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.